func testAfterQueuing(t *testing.T, delta Duration) error { // make the result channel buffered because we don't want // to depend on channel queueing semantics that might // possibly change in the future. result := make(chan afterResult, len(slots)) t0 := Now() for _, slot := range slots { go await(slot, result, After(Duration(slot)*delta)) } var order []int var times []Time for range slots { r := <-result order = append(order, r.slot) times = append(times, r.t) } for i := range order { if i > 0 && order[i] < order[i-1] { return fmt.Errorf("After calls returned out of order: %v", order) } } for i, t := range times { dt := t.Sub(t0) target := Duration(order[i]) * delta if dt < target-delta/2 || dt > target+delta*10 { return fmt.Errorf("After(%s) arrived at %s, expected [%s,%s]", target, dt, target-delta/2, target+delta*10) } } return nil }
func TestNatSub(t *testing.T) { tester = t test_msg = "NatSubA" nat_eq(0, nat_zero.Sub(nat_zero), nat_zero) nat_eq(1, c.Sub(nat_zero), c) test_msg = "NatSubB" for i := uint64(0); i < 100; i++ { t := sum(i, c) for j := uint64(0); j <= i; j++ { t = t.Sub(mul(Nat(j), c)) } nat_eq(uint(i), t, nat_zero) } }
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) }) }) }