Esempio n. 1
0
func Test_EventLoop(t *testing.T) {
	zone, _ := CreateZone("zone")
	room, _ := CreateRoom(zone, database.Coordinate{X: 0, Y: 0, Z: 0})
	user := CreateUser("user", "password")
	char := CreatePlayer("char", user, room)

	eventChannel := Register(char)

	message := "hey how are yah"
	queueEvent(TellEvent{char, char, message})

	timeout := testutils.Timeout(3 * time.Second)

	select {
	case event := <-eventChannel:
		tu.Assert(event.Type() == TellEventType, t, "Didn't get a Tell event back")
		tellEvent := event.(TellEvent)
		tu.Assert(tellEvent.Message == message, t, "Didn't get the right message back:", tellEvent.Message, message)
	case <-timeout:
		tu.Assert(false, t, "Timed out waiting for tell event")
	}

	select {
	case event := <-eventChannel:
		tu.Assert(event.Type() == TimerEventType, t, "Expected to get a timer event")
	case <-timeout:
		tu.Assert(false, t, "Timed out waiting for timer event")
	}

	_cleanup(t)
}
Esempio n. 2
0
func Test_CombatLoop(t *testing.T) {
	zone, _ := CreateZone("zone")
	room, _ := CreateRoom(zone, database.Coordinate{X: 0, Y: 0, Z: 0})
	user := CreateUser("user", "password")

	char1 := CreatePlayer("char1", user, room)
	char2 := CreatePlayer("char2", user, room)

	eventChannel1 := Register(char1)
	// eventChannel2 := Register(char2)

	StartFight(char1, char2)
	// StartFight(char2, char1)

	verifyEvents := func(eventChannel chan Event) {
		timeout := testutils.Timeout(3 * time.Second)
		expectedTypes := make(map[EventType]bool)
		expectedTypes[CombatEventType] = true
		expectedTypes[CombatStartEventType] = true

		for {
			select {
			case event := <-eventChannel1:
				if event.Type() != TimerEventType {
					tu.Assert(expectedTypes[event.Type()] == true, t, "Unexpected event type:", event.Type())
					delete(expectedTypes, event.Type())
				}
			case <-timeout:
				tu.Assert(false, t, "Timed out waiting for combat event")
			}

			if len(expectedTypes) == 0 {
				break
			}
		}
	}

	verifyEvents(eventChannel1)
	// verifyEvents(eventChannel2)
}