Example #1
0
func TestTimer(t *testing.T) {

	signal := test.NewSignalTester(t)

	pipe := cellnet.NewEventPipe()

	evq := pipe.AddQueue()

	pipe.Start()

	const testTimes = 3

	var count int = testTimes

	cellnet.NewTimer(evq, time.Second, func(t *cellnet.Timer) {
		log.Debugln("timer 1 sec tick")

		signal.Done(1)

		count--

		if count == 0 {
			t.Stop()
			signal.Done(2)
		}
	})

	for i := 0; i < testTimes; i++ {
		signal.WaitAndExpect(1, "timer not tick")
	}

	signal.WaitAndExpect(2, "timer not stop")

}
Example #2
0
// TestHistogramConcurrentUpdateCount would expose data race problems with
// concurrent Update and Count calls on Sample when test is called with -race
// argument
func TestHistogramConcurrentUpdateCount(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}
	h := NewHistogram(NewUniformSample(100))
	for i := 0; i < 100; i++ {
		h.Update(int64(i))
	}
	start := make(chan struct{})
	quit := make(chan struct{})
	go func() {
		t := time.NewTicker(10 * time.Millisecond)
		close(start)
		for {
			select {
			case <-t.C:
				h.Update(rand.Int63())
			case <-quit:
				t.Stop()
				return
			}
		}
	}()
	<-start // wait for background goroutine to start its job
	for i := 0; i < 1000; i++ {
		h.Count()
		time.Sleep(5 * time.Millisecond)
	}
	quit <- struct{}{}
}
Example #3
0
// TestUniformSampleConcurrentUpdateCount would expose data race problems with
// concurrent Update and Count calls on Sample when test is called with -race
// argument
func TestUniformSampleConcurrentUpdateCount(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}
	s := NewUniformSample(100)
	for i := 0; i < 100; i++ {
		s.Update(int64(i))
	}
	quit := make(chan struct{})
	go func() {
		t := time.NewTicker(10 * time.Millisecond)
		for {
			select {
			case <-t.C:
				s.Update(rand.Int63())
			case <-quit:
				t.Stop()
				return
			}
		}
	}()
	for i := 0; i < 1000; i++ {
		s.Count()
		time.Sleep(5 * time.Millisecond)
	}
	quit <- struct{}{}
}
Example #4
0
func TestConnection(t *testing.T) {
	irccon1 := New("go-eventirc1", "go-eventirc1")
	irccon1.VerboseCallbackHandler = true
	irccon1.Debug = true
	irccon2 := New("go-eventirc2", "go-eventirc2")
	irccon2.VerboseCallbackHandler = true
	irccon2.Debug = true
	err := irccon1.Connect("irc.yolo-swag.com:6667")
	if err != nil {
		t.Log(err.Error())
		t.Fatal("Can't connect to ShadowNET.")
	}
	err = irccon2.Connect("irc.yolo-swag.com:6667")
	if err != nil {
		t.Log(err.Error())
		t.Fatal("Can't connect to ShadowNET.")
	}
	irccon1.AddCallback("001", func(e *Event) { irccon1.Join("#go-eventirc") })
	irccon2.AddCallback("001", func(e *Event) { irccon2.Join("#go-eventirc") })
	con2ok := false
	irccon1.AddCallback("366", func(e *Event) {
		t := time.NewTicker(1 * time.Second)
		i := 10
		for {
			<-t.C
			irccon1.Privmsgf("#go-eventirc", "Test Message%d\n", i)
			if con2ok {
				i--
			}
			if i == 0 {
				t.Stop()
				irccon1.Quit()
			}
		}
	})

	irccon2.AddCallback("366", func(e *Event) {
		irccon2.Privmsg("#go-eventirc", "Test Message\n")
		con2ok = true
		irccon2.Nick("go-eventnewnick")
	})

	irccon2.AddCallback("PRIVMSG", func(e *Event) {
		t.Log(e.Message())
		if e.Message() == "Test Message5" {
			irccon2.Quit()
		}
	})

	irccon2.AddCallback("NICK", func(e *Event) {
		if irccon2.nickcurrent == "go-eventnewnick" {
			t.Fatal("Nick change did not work!")
		}
	})
	go irccon2.Loop()
	irccon1.Loop()
}
Example #5
0
func TestSystemTimer(t *testing.T) {
	t.Parallel()

	Convey(`A systemTimer instance`, t, func() {
		t := new(systemTimer)

		Convey(`Should start with a nil channel.`, func() {
			So(t.GetC(), ShouldBeNil)
		})

		Convey(`When stopped, should return inactive.`, func() {
			So(t.Stop(), ShouldBeFalse)
		})

		Convey(`When reset`, func() {
			active := t.Reset(1 * time.Hour)
			So(active, ShouldBeFalse)

			Convey(`When reset to a short duration`, func() {
				// Upper bound of supported platform resolution. Windows is 15ms, so
				// make sure we exceed that.
				active := t.Reset(100 * time.Millisecond)

				Convey(`Should return active.`, func() {
					So(active, ShouldBeTrue)
				})

				Convey(`Should trigger shortly.`, func() {
					tm := <-t.GetC()
					So(tm, ShouldNotResemble, time.Time{})
				})
			})

			Convey(`When stopped, should return active and have a non-nil C.`, func() {
				active := t.Stop()
				So(active, ShouldBeTrue)
				So(t.GetC(), ShouldNotBeNil)
			})

			Convey(`When stopped, should return active.`, func() {
				active := t.Stop()
				So(active, ShouldBeTrue)
			})

			Convey(`Should have a non-nil channel.`, func() {
				So(t.GetC(), ShouldNotBeNil)
			})

			Reset(func() {
				t.Stop()
			})
		})
	})
}