示例#1
0
func TestWaiter_Add(t *testing.T) {
	c := make(chan struct{}, 1)
	done := make(chan struct{}, 1)
	w := &Waiter{}
	w.Add(c)
	close(c)
	w.Go(done)
	assert.WaitFor(t, done, false, "")

	w = &Waiter{}
	done = make(chan struct{}, 1)
	w.Go(done)
	assert.WaitFor(t, done, false, "")
}
示例#2
0
func TestWatchMulti(t *testing.T) {
	done := make(chan struct{})
	go func() {
		// We watch two channels, a and b
		a := make(chan struct{}, 1)
		b := make(chan struct{}, 1)
		c := Multi(a, b)

		// We send a signal to a and wait for the response on the
		a <- struct{}{}
		assert.WaitFor(t, c, true, "A")

		// We send a signal to a and b and ensure two signals are returned
		a <- struct{}{}
		b <- struct{}{}
		assert.WaitFor(t, c, true, "B")
		assert.WaitFor(t, c, true, "C")

		// We close a and send on b to ensure it still works
		close(a)
		b <- struct{}{}
		assert.WaitFor(t, c, true, "D")

		// We close the second input channel, and check that the signal channel is also closed
		close(b)
		assert.WaitFor(t, c, false, "E")

		close(done)
	}()
	assert.WaitFor(t, done, false, "G")
}
示例#3
0
文件: notifier_test.go 项目: kego/ke
func TestNotifier_Notify(t *testing.T) {
	n := &Notifier{}
	done := make(chan struct{}, 1)

	// subscribers should be nil, so will return 0
	finished := n.Notify("a", bNotif)
	assert.WaitFor(t, finished, false)

	// subscribers should be nil, so will return 0
	count, finished := n.notifyCount("a", bNotif, nil)
	assert.WaitFor(t, finished, false)
	assert.Equal(t, 0, count)

	c := n.Watch("a", bNotif)
	go func() {
		notif := <-c
		close(notif.Done)
		close(done)
	}()

	// notif doesn't exist so will return 0
	count, finished = n.notifyCount("a", cNotif, nil)
	assert.WaitFor(t, finished, false)
	assert.Equal(t, 0, count)

	// object doesn't exist so will return 0
	count, finished = n.notifyCount("b", bNotif, nil)
	assert.WaitFor(t, finished, false)
	assert.Equal(t, 0, count)

	// should notify
	count, finished = n.notifyCount("a", bNotif, nil)
	assert.WaitFor(t, finished, false)
	assert.Equal(t, 1, count)
	assert.WaitFor(t, done, false, "Timed out waiting for notify1")

	done = make(chan struct{}, 1)
	go func() {
		notif := <-c
		s, ok := notif.Data.(string)
		assert.True(t, ok)
		assert.Equal(t, "h", s)
		close(notif.Done)
		close(done)
	}()
	count, finished = n.notifyCount(nil, bNotif, "h")
	assert.WaitFor(t, finished, false)
	assert.Equal(t, 1, count)
	assert.WaitFor(t, done, false, "Timed out waiting for notify2")

	d := n.Watch(nil, eNotif)
	done = make(chan struct{}, 1)
	go func() {
		notif := <-d
		close(notif.Done)
		close(done)
	}()
	count, finished = n.notifyCount("f", eNotif, nil)
	assert.WaitFor(t, finished, false)
	assert.Equal(t, 1, count)
	assert.WaitFor(t, done, false, "Timed out waiting for notify3")

	g := n.Watch("h", iNotif, jNotif)
	done = make(chan struct{}, 1)
	go func() {
		notif1 := <-g
		close(notif1.Done)
		notif2 := <-g
		close(notif2.Done)
		close(done)
	}()
	count, finished = n.notifyCount("h", jNotif, nil)
	assert.WaitFor(t, finished, false)
	assert.Equal(t, 1, count)
	count, finished = n.notifyCount("h", iNotif, nil)
	assert.WaitFor(t, finished, false)
	assert.Equal(t, 1, count)
	assert.WaitFor(t, done, false, "Timed out waiting for notify4")

	done = make(chan struct{}, 1)
	go func() {
		notif := <-c
		s, ok := notif.Data.(string)
		assert.True(t, ok)
		assert.Equal(t, "i", s)
		close(notif.Done)
		close(done)
	}()
	finished = n.NotifyWithData(nil, bNotif, "i")
	assert.WaitFor(t, finished, false)
	assert.WaitFor(t, done, false, "Timed out waiting for notify5")
}