Ejemplo n.º 1
0
func TestToil(t *testing.T) {

	// Initialize.
	randomness := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))

	// Do tests.
	const NUM_TOIL_TESTS = 20
	for testNumber := 0; testNumber < NUM_TOIL_TESTS; testNumber++ {

		numberOfTimesToToil := randomness.Intn(44)

		var waitGroup sync.WaitGroup
		waitGroup.Add(numberOfTimesToToil)

		toiler := toiltest.NewRecorder()
		toiler.ToilFunc(func() {
			waitGroup.Done()
		})

		group := NewGroup()

		for i := 0; i < numberOfTimesToToil; i++ {
			group.Register(toiler)
		}

		go group.Toil()

		waitGroup.Wait() // Make sure all the calls on the Toil() method are done before continuing.

		if expected, actual := numberOfTimesToToil, toiler.NumToiling(); expected != actual {
			t.Errorf("For test #%d with, expected the number of toiling toilers to be %d, but actually was %d.", testNumber, expected, actual)
			continue
		}
	}
}
Ejemplo n.º 2
0
func TestReturnedNotice(t *testing.T) {

	toiler := toiltest.NewRecorder()

	numReturned := 0

	var waitGroup sync.WaitGroup
	waitGroup.Add(1)
	toiler.ReturnedNoticeFunc(func() {
		numReturned++
		waitGroup.Done()
	})

	retoiler := New(toiler, DelayedLimitedStrategy(2, 0))

	if expected, actual := 0, numReturned; expected != actual {
		t.Errorf("Expected number of times returned to be %d, but actually was %d.", expected, actual)
	}

	go retoiler.Toil()

	toiler.Terminate()
	waitGroup.Wait()

	if expected, actual := 1, numReturned; expected != actual {
		t.Errorf("Expected number of times returned to be %d, but actually was %d.", expected, actual)
	}
}
Ejemplo n.º 3
0
func TestPanickedNotice(t *testing.T) {

	toiler := toiltest.NewRecorder()

	numPanicked := 0

	var waitGroup sync.WaitGroup
	var caughtPanicValue interface{}
	waitGroup.Add(1)
	toiler.PanickedNoticeFunc(func(panicValue interface{}) {
		numPanicked++
		caughtPanicValue = panicValue
		waitGroup.Done()
	})

	retoiler := New(toiler, DelayedLimitedStrategy(2, 0))

	if expected, actual := 0, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}

	go retoiler.Toil()

	panicValue := "Panic panic, panic!!!!"
	toiler.Panic(panicValue)
	waitGroup.Wait()

	if expected, actual := 1, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}
	if expected, actual := panicValue, caughtPanicValue; expected != actual {
		t.Errorf("Expected caught panic value to be [%v], but actually was [%v].", expected, actual)
	}
}
Ejemplo n.º 4
0
func TestRegisterCh(t *testing.T) {

	toiler := toiltest.NewRecorder()

	panicCh := make(chan interface{})

	daemon := newGroupDaemon(panicCh)

	const NUM_REGISTER_TESTS = 20
	doneCh := make(chan struct{})
	for testNumber := 0; testNumber < NUM_REGISTER_TESTS; testNumber++ {
		daemon.RegisterCh() <- struct {
			doneCh chan struct{}
			toiler Toiler
		}{
			doneCh: doneCh,
			toiler: toiler,
		}
		<-doneCh // THE TEST WE ARE DOING IS MAKING SURE THIS DOES NOT RESULT IN A DEADLOCK.
		// THUS WE ARE NOT CALLING ANYTHING LIKE t.Errorf() IN THIS CASE.

		lengthReturnCh := make(chan int)
		daemon.LengthCh() <- struct{ returnCh chan int }{
			returnCh: lengthReturnCh,
		}
		length := <-lengthReturnCh

		if expected, actual := 1+testNumber, length; expected != actual {
			t.Errorf("For test #%d, after registering toilers with new daemon, expected the number of registered toilers to be %d, but actually was %d.", testNumber, expected, actual)
			continue
		}
	}
}
Ejemplo n.º 5
0
func TestNew(t *testing.T) {

	toiler := toiltest.NewRecorder()

	retoiler := New(toiler, NeverStrategy())
	if nil == retoiler {
		t.Errorf("After trying to create a new retoiler, received nil: %v", retoiler)
	}
}
Ejemplo n.º 6
0
func TestToilCh(t *testing.T) {

	// Initialize.
	randomness := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))

	// Do tests.
	const NUM_TOIL_TESTS = 20
	doneCh := make(chan struct{})
	for testNumber := 0; testNumber < NUM_TOIL_TESTS; testNumber++ {

		numberOfTimesToToil := randomness.Intn(44)

		var waitGroup sync.WaitGroup
		waitGroup.Add(numberOfTimesToToil)

		toiler := toiltest.NewRecorder()
		toiler.ToilFunc(func() {
			waitGroup.Done()
		})

		panicCh := make(chan interface{})

		daemon := newGroupDaemon(panicCh)

		for i := 0; i < numberOfTimesToToil; i++ {

			daemon.RegisterCh() <- struct {
				doneCh chan struct{}
				toiler Toiler
			}{
				doneCh: doneCh,
				toiler: toiler,
			}
			<-doneCh // THE TEST WE ARE DOING IS MAKING SURE THIS DOES NOT RESULT IN A DEADLOCK.
			// THUS WE ARE NOT CALLING ANYTHING LIKE t.Errorf() IN THIS CASE.
		}

		daemon.ToilCh() <- struct{ doneCh chan struct{} }{
			doneCh: doneCh,
		}
		<-doneCh // THE TEST WE ARE DOING IS MAKING SURE THIS DOES NOT RESULT IN A DEADLOCK.
		// THUS WE ARE NOT CALLING ANYTHING LIKE t.Errorf() IN THIS CASE.

		waitGroup.Wait() // Make sure all the calls on the Toil() method are done before continuing.

		if expected, actual := numberOfTimesToToil, toiler.NumToiling(); expected != actual {
			t.Errorf("For test #%d with, expected the number of toiling toilers to be %d, but actually was %d.", testNumber, expected, actual)
			continue
		}
	}
}
Ejemplo n.º 7
0
func TestToil(t *testing.T) {

	toiler := toiltest.NewRecorder()

	numToiled := 0

	var waitGroup sync.WaitGroup
	waitGroup.Add(1)
	toiler.ToilFunc(func() {
		numToiled++
		waitGroup.Done()
	})

	retoiler := New(toiler, DelayedLimitedStrategy(2, 0)) // NOTE that the 2 means we can only panic() this toiler 2 times!

	if expected, actual := 0, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	go retoiler.Toil()

	waitGroup.Wait()

	if expected, actual := 1, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	waitGroup.Add(1)
	panicValue1 := "Panic panic, panic!!!! [1]"
	toiler.Panic(panicValue1)
	waitGroup.Wait()

	if expected, actual := 2, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	waitGroup.Add(1)
	panicValue2 := "Panic panic, panic!!!! [2]"
	toiler.Panic(panicValue2)
	waitGroup.Wait()

	if expected, actual := 3, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}
}
Ejemplo n.º 8
0
func TestRegister(t *testing.T) {

	toiler := toiltest.NewRecorder()

	group := NewGroup()

	const NUM_REGISTER_TESTS = 20
	for testNumber := 0; testNumber < NUM_REGISTER_TESTS; testNumber++ {

		group.Register(toiler)

		length := group.Len()

		if expected, actual := 1+testNumber, length; expected != actual {
			t.Errorf("For test #%d, after registering toilers with new group, expected the number of registered toilers to be %d, but actually was %d.", testNumber, expected, actual)
			continue
		}
	}
}
Ejemplo n.º 9
0
func TestFork(t *testing.T) {

	toiler := toiltest.NewRecorder()

	newFn := func() (toil.Toiler, error) {
		return toiler, nil
	}

	toilForker := NewFunc(newFn)

	const NUM_FORK_TESTS = 20
	for testNumber := 0; testNumber < NUM_FORK_TESTS; testNumber++ {

		toilForker.Fork()

		length := toilForker.Len()

		if expected, actual := 1+testNumber, length; expected != actual {
			t.Errorf("For test #%d, after fork, expected the number of toilers to be %d, but actually was %d.", testNumber, expected, actual)
			continue
		}
	}
}
Ejemplo n.º 10
0
func TestToilPanicked(t *testing.T) {

	// Initialize.
	randomness := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))

	// Do test.
	toiler := toiltest.NewRecorder()

	numToiled := 0
	var toiledWaitGroup sync.WaitGroup
	toiledWaitGroup.Add(1)
	toiler.ToilFunc(func() {
		numToiled++
		toiledWaitGroup.Done()
	})

	numPanicked := 0
	var panickedWaitGroup sync.WaitGroup
	toiler.PanickedNoticeFunc(func(panicValue interface{}) {
		numPanicked++
		panickedWaitGroup.Done()
	})

	group := NewGroup()

	group.Register(toiler)

	var expectedPanicValue interface{} = nil
	go func() {
		defer func() {
			if expected, actual := 1, numToiled; expected != actual {
				t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
			}

			if panicValue := recover(); nil != panicValue {
				if expected, actual := panicValue, expectedPanicValue; expected != actual {
					t.Errorf("Expected caught panic value to be [%v], but actually was [%v].", expected, actual)
				}
			} else {
				t.Errorf("This should NOT get to this part of the code either!!")
			}
		}()

		group.Toil()
	}()
	toiledWaitGroup.Wait() // Make sure all the calls on the Toil() method are done before continuing.

	if expected, actual := 1, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	if expected, actual := 0, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}

	panickedWaitGroup.Add(1)
	panicValue := fmt.Sprintf("Panic Value with some random stuff: %d", randomness.Intn(999999999))
	expectedPanicValue = panicValue // <----------------- NOTE we set the expectedPanicValue
	toiler.Panic(panicValue)
	panickedWaitGroup.Wait()

	//                     V---------- NOTE that sayed as 1.
	if expected, actual := 1, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	if expected, actual := 1, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}
}
func TestRetoilWithDelayedLimitedStrategy(t *testing.T) {

	toiler := toiltest.NewRecorder()

	numToiled := 0
	var toiledWaitGroup sync.WaitGroup
	toiledWaitGroup.Add(1)
	toiler.ToilFunc(func() {
		numToiled++
		toiledWaitGroup.Done()
	})

	numPanicked := 0
	var panickedWaitGroup sync.WaitGroup
	toiler.PanickedNoticeFunc(func(panicValue interface{}) {
		numPanicked++
		panickedWaitGroup.Done()
	})

	retoiler := New(toiler, DelayedLimitedStrategy(2, 0)) // NOTE that the 2 means we can only panic() this toiler 2 times before panic()s get through!

	if expected, actual := 0, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	if expected, actual := 0, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}

	var finalPanicValue interface{} = nil
	go func() {
		defer func() {
			if expected, actual := 3, numToiled; expected != actual {
				t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
			}

			if panicValue := recover(); nil != panicValue {
				if expected, actual := panicValue, finalPanicValue; expected != actual {
					t.Errorf("Expected caught panic value to be [%v], but actually was [%v].", expected, actual)
				}
			} else {
				t.Errorf("This should NOT get to this part of the code either!!")
			}
		}()

		retoiler.Toil()
	}()

	toiledWaitGroup.Wait()

	if expected, actual := 1, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	if expected, actual := 0, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}

	toiledWaitGroup.Add(1)
	panickedWaitGroup.Add(1)
	panicValue1 := "Panic panic, panic!!!! [1]"
	toiler.Panic(panicValue1)
	toiledWaitGroup.Wait()
	panickedWaitGroup.Wait()

	if expected, actual := 2, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	if expected, actual := 1, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}

	toiledWaitGroup.Add(1)
	panickedWaitGroup.Add(1)
	panicValue2 := "Panic panic, panic!!!! [2]"
	toiler.Panic(panicValue2)
	toiledWaitGroup.Wait()
	panickedWaitGroup.Wait()

	if expected, actual := 3, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	if expected, actual := 2, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}

	panickedWaitGroup.Add(1)
	panicValue3 := "Panic panic, panic!!!! [3]"
	finalPanicValue = panicValue3 // <----------------- NOTE we set the finalPanicValue
	toiler.Panic(panicValue3)
	panickedWaitGroup.Wait()

	//                     V---------- NOTE that sayed as 3.
	if expected, actual := 3, numToiled; expected != actual {
		t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
	}

	if expected, actual := 3, numPanicked; expected != actual {
		t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
	}
}