Example #1
0
func TestEventual(t *testing.T) {
	Convey("eventual basic functionality, no mandatory, no exclusive", t, func() {
		e := New()

		t, err := e.Register("test.topic", false, false)
		So(err, ShouldBeNil)
		So(t.GetTopic(), ShouldEqual, "test.topic")
		So(t.IsMandatory(), ShouldBeFalse)
		So(t.IsExclusive(), ShouldBeFalse)

		Convey("pub/sub test", func() {
			s1 := t.Sub()
			s2 := t.Sub()
			t.Sub() // This must pass, since its not mandatory
			wg := sync.WaitGroup{}
			wg.Add(2)

			var resp1, resp2 IEvent
			go func() {
				defer wg.Done()
				resp1 = <-s1
			}()

			go func() {
				defer wg.Done()
				resp2 = <-s2
			}()

			// Make sure the readers are ready. I know this is not accurate, but
			// I don't know any beter way
			time.Sleep(time.Second)
			t.Pub(mockIEvent{1})

			wg.Wait()
			So(resp1.Data().(int), ShouldEqual, 1)
			So(resp2.Data().(int), ShouldEqual, 1)
		})

		Convey("pub/sub test on another channel", func() {
			t1, err := e.Register("test.topic", false, false)
			So(err, ShouldBeNil)

			s1 := t.Sub()
			wg := sync.WaitGroup{}
			wg.Add(1)

			var resp IEvent
			go func() {
				defer wg.Done()
				resp = <-s1
			}()
			time.Sleep(time.Second)

			t1.Pub(mockIEvent{2})

			wg.Wait()
			So(resp.Data().(int), ShouldEqual, 2)
		})
	})
}