Пример #1
0
func (s *EventSuite) TestEventLoop(c *C) {

	char := testutils.NewMockPC()

	eventChannel := Register(char)

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

	select {
	case event := <-eventChannel:
		gotTellEvent := false

		switch e := event.(type) {
		case TellEvent:
			gotTellEvent = true
			c.Assert(e.Message, Equals, message)

		}

		if gotTellEvent == false {
			c.Fatalf("Didn't get a Tell event back")
		}
	case <-time.After(3 * time.Second):
		c.Fatalf("Timed out waiting for tell event")
	}
}
Пример #2
0
func (s *CombatSuite) TestCombatLoop(c *C) {
	char1 := testutils.NewMockPC()
	char2 := testutils.NewMockPC()
	char1.RoomId = char2.RoomId

	eventChannel1 := events.Register(char1)
	eventChannel2 := events.Register(char2)

	StartFight(char1, nil, char2)

	c.Assert(InCombat(char1), Equals, true)
	c.Assert(InCombat(char2), Equals, true)

	verifyEvents := func(channel chan events.Event) {
		gotCombatEvent := false
		gotStartEvent := false

	Loop:
		for {
			select {
			case event := <-channel:
				switch event.(type) {
				case events.TickEvent:
				case events.CombatEvent:
					gotCombatEvent = true
				case events.CombatStartEvent:
					gotStartEvent = true
				default:
					c.FailNow()
				}
			case <-time.After(30 * time.Millisecond):
				c.Fatalf("Timed out waiting for combat event")
				break Loop
			}

			if gotCombatEvent && gotStartEvent {
				break
			}
		}
	}
	verifyEvents(eventChannel1)
	verifyEvents(eventChannel2)

	StopFight(char1)

	e := <-eventChannel1
	switch e.(type) {
	case events.CombatStopEvent:
	default:
		c.Fatalf("Didn't get a combat stop event (channel 1)")
	}

	e = <-eventChannel2
	switch e.(type) {
	case events.CombatStopEvent:
	default:
		c.Fatalf("Didn't get a combat stop event (channel 1)")
	}

	select {
	case e := <-eventChannel1:
		c.Fatalf("Shouldn't have gotten any combat events after stopping combat (channel 1) - %s", e)
	case e := <-eventChannel2:
		c.Fatalf("Shouldn't have gotten any combat events after stopping combat (channel 2) - %s", e)
	case <-time.After(20 * time.Millisecond):
	}

	StartFight(char1, nil, char2)
	<-eventChannel1
	<-eventChannel2
}