Exemple #1
0
// Test an error in a deferred function inside the loop.
func TestDeferredError(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	done := false
	l := loop.Go(generateDeferredErrorBackend(&done))

	assert.ErrorMatch(l.Stop(), "deferred error", "error has to be 'deferred error'")
	assert.True(done, "backend has done")

	status, _ := l.Error()

	assert.Equal(loop.Stopped, status, "loop is stopped")
}
Exemple #2
0
// Test the simple backend returning nil after stop.
func TestSimpleStop(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	done := false
	l := loop.Go(generateSimpleBackend(&done))

	assert.Nil(l.Stop(), "no error after simple stop")
	assert.True(done, "backend has done")

	status, _ := l.Error()

	assert.Equal(loop.Stopped, status, "loop is stopped")
}
Exemple #3
0
// StartLimited creates and runs a new scene with an inactivity
// and an absolute timeout. They may be zero.
func StartLimited(inactivity, absolute time.Duration) Scene {
	s := &scene{
		id:          identifier.NewUUID(),
		props:       make(map[string]*box),
		flags:       make(map[string]bool),
		signalings:  make(map[string][]chan struct{}),
		inactivity:  inactivity,
		absolute:    absolute,
		commandChan: make(chan *envelope, 1),
	}
	s.backend = loop.Go(s.backendLoop)
	return s
}
Exemple #4
0
// Test an internal error.
func TestError(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	done := false
	l := loop.Go(generateErrorBackend(&done))

	time.Sleep(longDelay)

	assert.ErrorMatch(l.Stop(), "timed out", "error has to be 'time out'")
	assert.True(done, "backend has done")

	status, _ := l.Error()

	assert.Equal(loop.Stopped, status, "loop is stopped")
}
Exemple #5
0
// Test the simple backend returning an error after kill.
func TestSimpleKill(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	done := false
	l := loop.Go(generateSimpleBackend(&done))

	l.Kill(errors.New("ouch"))

	assert.ErrorMatch(l.Stop(), "ouch", "error has to be 'ouch'")
	assert.True(done, "backend has done")

	status, _ := l.Error()

	assert.Equal(loop.Stopped, status, "loop is stopped")
}
Exemple #6
0
func ExampleLoopFunc() {
	printChan := make(chan string)
	loopFunc := func(l loop.Loop) error {
		for {
			select {
			case <-l.ShallStop():
				return nil
			case str := <-printChan:
				println(str)
			}
		}
	}
	loop.Go(loopFunc)
}