Example #1
0
// TestSimpleNoTimeout tests a simple scene usage without
// any timeout.
func TestSimpleNoTimeout(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, false)
	scn := scene.Start()

	id := scn.ID()
	assert.Length(id, 16)
	err := scn.Store("foo", 4711)
	assert.Nil(err)
	foo, err := scn.Fetch("foo")
	assert.Nil(err)
	assert.Equal(foo, 4711)
	_, err = scn.Fetch("bar")
	assert.True(scene.IsPropNotFoundError(err))
	err = scn.Store("foo", "bar")
	assert.True(scene.IsPropAlreadyExistError(err))
	_, err = scn.Dispose("bar")
	assert.True(scene.IsPropNotFoundError(err))
	foo, err = scn.Dispose("foo")
	assert.Nil(err)
	assert.Equal(foo, 4711)
	_, err = scn.Fetch("foo")
	assert.True(scene.IsPropNotFoundError(err))

	status, err := scn.Status()
	assert.Nil(err)
	assert.Equal(status, scene.Active)

	err = scn.Stop()
	assert.Nil(err)

	status, err = scn.Status()
	assert.Nil(err)
	assert.Equal(status, scene.Over)
}
Example #2
0
// TestCleanupNoError tests the cleanup of props with
// no errors.
func TestCleanupNoError(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, false)
	cleanups := make(map[string]interface{})
	cleanup := func(key string, prop interface{}) error {
		cleanups[key] = prop
		return nil
	}
	scn := scene.Start()

	err := scn.StoreClean("foo", 4711, cleanup)
	assert.Nil(err)
	err = scn.StoreClean("bar", "yadda", cleanup)
	assert.Nil(err)

	foo, err := scn.Dispose("foo")
	assert.Nil(err)
	assert.Equal(foo, 4711)

	err = scn.Stop()
	assert.Nil(err)

	assert.Length(cleanups, 2)
	assert.Equal(cleanups["foo"], 4711)
	assert.Equal(cleanups["bar"], "yadda")
}
Example #3
0
// TestEarlyFlag tests the signaling before a waiting.
func TestEarlyFlag(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, false)
	scn := scene.Start()
	err := scn.Flag("foo")
	assert.Nil(err)

	go func() {
		err := scn.WaitFlag("foo")
		assert.Nil(err)
		err = scn.Store("foo-a", true)
		assert.Nil(err)
	}()
	go func() {
		err := scn.WaitFlag("foo")
		assert.Nil(err)
		err = scn.Store("foo-b", true)
		assert.Nil(err)
	}()

	time.Sleep(100 * time.Millisecond)

	fooA, err := scn.Fetch("foo-a")
	assert.Nil(err)
	assert.Equal(fooA, true)
	fooB, err := scn.Fetch("foo-b")
	assert.Nil(err)
	assert.Equal(fooB, true)

	err = scn.Stop()
	assert.Nil(err)
}
Example #4
0
// TestSimpleInactivityTimeout tests a simple scene usage
// with inactivity timeout.
func TestSimpleInactivityTimeout(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, false)
	scn := scene.StartLimited(100*time.Millisecond, 0)

	err := scn.Store("foo", 4711)
	assert.Nil(err)

	for i := 0; i < 5; i++ {
		foo, err := scn.Fetch("foo")
		assert.Nil(err)
		assert.Equal(foo, 4711)
		time.Sleep(50)
	}

	time.Sleep(100 * time.Millisecond)

	foo, err := scn.Fetch("foo")
	assert.True(scene.IsTimeoutError(err))
	assert.Nil(foo)

	status, err := scn.Status()
	assert.True(scene.IsTimeoutError(err))
	assert.Equal(status, scene.Over)

	err = scn.Stop()
	assert.True(scene.IsTimeoutError(err))
}
Example #5
0
// Test UUID versions.
func TestUUIDVersions(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	ns := identifier.UUIDNamespaceOID()
	// Asserts.
	uuidV1, err := identifier.NewUUIDv1()
	assert.Nil(err)
	assert.Equal(uuidV1.Version(), identifier.UUIDv1)
	assert.Equal(uuidV1.Variant(), identifier.UUIDVariantRFC4122)
	assert.Logf("UUID V1: %v", uuidV1)
	uuidV3, err := identifier.NewUUIDv3(ns, []byte{4, 7, 1, 1})
	assert.Nil(err)
	assert.Equal(uuidV3.Version(), identifier.UUIDv3)
	assert.Equal(uuidV3.Variant(), identifier.UUIDVariantRFC4122)
	assert.Logf("UUID V3: %v", uuidV3)
	uuidV4, err := identifier.NewUUIDv4()
	assert.Nil(err)
	assert.Equal(uuidV4.Version(), identifier.UUIDv4)
	assert.Equal(uuidV4.Variant(), identifier.UUIDVariantRFC4122)
	assert.Logf("UUID V4: %v", uuidV4)
	uuidV5, err := identifier.NewUUIDv5(ns, []byte{4, 7, 1, 1})
	assert.Nil(err)
	assert.Equal(uuidV5.Version(), identifier.UUIDv5)
	assert.Equal(uuidV5.Variant(), identifier.UUIDVariantRFC4122)
	assert.Logf("UUID V5: %v", uuidV5)
}
Example #6
0
// Test time containments.
func TestTimeContainments(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	// Create some test data.
	ts := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
	years := []int{2008, 2009, 2010}
	months := []time.Month{10, 11, 12}
	days := []int{10, 11, 12, 13, 14}
	hours := []int{20, 21, 22, 23}
	minutes := []int{0, 5, 10, 15, 20, 25}
	seconds := []int{0, 15, 30, 45}
	weekdays := []time.Weekday{time.Monday, time.Tuesday, time.Wednesday}

	assert.True(timex.YearInList(ts, years), "Go time in year list.")
	assert.True(timex.YearInRange(ts, 2005, 2015), "Go time in year range.")
	assert.True(timex.MonthInList(ts, months), "Go time in month list.")
	assert.True(timex.MonthInRange(ts, 7, 12), "Go time in month range.")
	assert.True(timex.DayInList(ts, days), "Go time in day list.")
	assert.True(timex.DayInRange(ts, 5, 15), "Go time in day range .")
	assert.True(timex.HourInList(ts, hours), "Go time in hour list.")
	assert.True(timex.HourInRange(ts, 20, 31), "Go time in hour range .")
	assert.True(timex.MinuteInList(ts, minutes), "Go time in minute list.")
	assert.True(timex.MinuteInRange(ts, 0, 5), "Go time in minute range .")
	assert.True(timex.SecondInList(ts, seconds), "Go time in second list.")
	assert.True(timex.SecondInRange(ts, 0, 5), "Go time in second range .")
	assert.True(timex.WeekdayInList(ts, weekdays), "Go time in weekday list.")
	assert.True(timex.WeekdayInRange(ts, time.Monday, time.Friday), "Go time in weekday range .")
}
Example #7
0
// TestFlagTimeout tests the waiting for a signal with
// a timeout.
func TestFlagTimeout(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, false)
	scn := scene.Start()

	go func() {
		err := scn.WaitFlag("foo")
		assert.Nil(err)
		err = scn.Store("foo-a", true)
		assert.Nil(err)
	}()
	go func() {
		err := scn.WaitFlagLimited("foo", 50*time.Millisecond)
		assert.True(scene.IsWaitedTooLongError(err))
		err = scn.Store("foo-b", true)
		assert.Nil(err)
	}()

	time.Sleep(100 * time.Millisecond)

	err := scn.Flag("foo")
	assert.Nil(err)

	fooA, err := scn.Fetch("foo-a")
	assert.Nil(err)
	assert.Equal(fooA, true)
	fooB, err := scn.Fetch("foo-b")
	assert.Nil(err)
	assert.Equal(fooB, true)

	err = scn.Stop()
	assert.Nil(err)
}
Example #8
0
// Test of the ETM monitor.
func TestEtmMonitor(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	// Generate measurings.
	for i := 0; i < 500; i++ {
		n := rand.Intn(10)
		id := fmt.Sprintf("mp:task:%d", n)
		m := monitoring.BeginMeasuring(id)
		work(n * 5000)
		m.EndMeasuring()
	}
	// Need some time to let that backend catch up queued mesurings.
	time.Sleep(time.Millisecond)
	// Asserts.
	mp, err := monitoring.ReadMeasuringPoint("foo")
	assert.ErrorMatch(err, `\[MONITORING:.*\] measuring point "foo" does not exist`, "reading non-existent measuring point")
	mp, err = monitoring.ReadMeasuringPoint("mp:task:5")
	assert.Nil(err, "No error expected.")
	assert.Equal(mp.Id, "mp:task:5", "should get the right one")
	assert.True(mp.Count > 0, "should be measured several times")
	assert.Match(mp.String(), `Measuring Point "mp:task:5" \(.*\)`, "string representation should look fine")
	monitoring.MeasuringPointsDo(func(mp *monitoring.MeasuringPoint) {
		assert.Match(mp.Id, "mp:task:[0-9]", "id has to match the pattern")
		assert.True(mp.MinDuration <= mp.AvgDuration && mp.AvgDuration <= mp.MaxDuration,
			"avg should be somewhere between min and max")
	})
}
Example #9
0
// Test the creation of identifiers based on parts with defined seperators.
func TestSepIdentifier(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)

	id := identifier.SepIdentifier("+", 1, "oNe", 2, "TWO", "3", "ÄÖÜ")
	assert.Equal(id, "1+one+2+two+3+äöü", "wrong SepIdentifier() result")

	id = identifier.LimitedSepIdentifier("+", true, "     ", 1, "oNe", 2, "TWO", "3", "ÄÖÜ", "Four", "+#-:,")
	assert.Equal(id, "1+one+2+two+3+four", "wrong LimitedSepIdentifier() result")
}
Example #10
0
// Test log level.
func TestLevel(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)

	logger.SetLevel(logger.LevelDebug)
	assert.Equal(logger.Level(), logger.LevelDebug, "Level debug.")
	logger.SetLevel(logger.LevelCritical)
	assert.Equal(logger.Level(), logger.LevelCritical, "Level critical.")
	logger.SetLevel(logger.LevelDebug)
	assert.Equal(logger.Level(), logger.LevelDebug, "Level debug.")
}
Example #11
0
// Test the creation of identifiers based on parts.
func TestIdentifier(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)

	// Identifier.
	id := identifier.Identifier("One", 2, "three four")
	assert.Equal(id, "one:2:three-four", "wrong Identifier() result")

	id = identifier.Identifier(2011, 6, 22, "One, two, or  three things.")
	assert.Equal(id, "2011:6:22:one-two-or-three-things", "wrong Identifier() result")
}
Example #12
0
// Test creating UUIDs from hex strings.
func TestUUIDByHex(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	// Asserts.
	_, err := identifier.NewUUIDByHex("ffff")
	assert.ErrorMatch(err, `\[IDENTIFIER:.*\] invalid length of hex string, has to be 32`)
	_, err = identifier.NewUUIDByHex("012345678901234567890123456789zz")
	assert.ErrorMatch(err, `\[IDENTIFIER:.*\] invalid value of hex string: .*`)
	_, err = identifier.NewUUIDByHex("012345678901234567890123456789ab")
	assert.Nil(err)
}
Example #13
0
// Test log level.
func TestGetSetLevel(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)

	logger.SetLevel(logger.LevelDebug)
	assert.Equal(logger.Level(), logger.LevelDebug)
	logger.SetLevel(logger.LevelCritical)
	assert.Equal(logger.Level(), logger.LevelCritical)
	logger.SetLevel(logger.LevelDebug)
	assert.Equal(logger.Level(), logger.LevelDebug)
}
Example #14
0
// Test the behavior after an internal panic.
func TestInternalPanic(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	// Register monitoring func with panic.
	monitoring.Register("panic", func() (string, error) { panic("ouch"); return "panic", nil })
	// Need some time to let that backend catch up queued registering.
	time.Sleep(time.Millisecond)
	// Asserts.
	dsv, err := monitoring.ReadStatus("panic")
	assert.Empty(dsv, "no dynamic status value")
	assert.ErrorMatch(err, `\[MONITORING:.*\] monitor backend panicked`, "monitor restarted due to panic")
}
Example #15
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")
}
Example #16
0
// Test the creation of identifiers based on types.
func TestTypeAsIdentifierPart(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)

	// Type as identifier.
	var tai TypeToSplitForIdentifier

	id := identifier.TypeAsIdentifierPart(tai)
	assert.Equal(id, "type-to-split-for-identifier", "wrong TypeAsIdentifierPart() result")

	id = identifier.TypeAsIdentifierPart(identifier.NewUUID())
	assert.Equal(id, "u-u-i-d", "wrong TypeAsIdentifierPart() result")
}
Example #17
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")
}
Example #18
0
// Test crontab keeping the job.
func TestCrontabKeep(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	// Create test crontab with job.
	c := timex.NewCrontab(10 * time.Millisecond)
	j := &cronjob{0, false, false}

	c.Add("keep", j)
	time.Sleep(50 * time.Millisecond)
	c.Remove("keep")
	c.Stop()

	assert.Equal(j.counter, 3, "job counter increased twice")
}
Example #19
0
// Test crontab removing the job after an error.
func TestCrontabError(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	// Create test crontab with job.
	c := timex.NewCrontab(10 * time.Millisecond)
	j := &cronjob{0, false, true}

	c.Add("remove", j)
	time.Sleep(250 * time.Millisecond)
	c.Remove("remove")
	c.Stop()

	assert.Equal(j.counter, 5, "job counter increased max five times")
}
Example #20
0
// Test the annotation of errors.
func TestAnnotation(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)

	ec := 123
	messages := errors.Messages{ec: "annotated"}
	aerr := testError("wrapped")
	err := errors.Annotate(aerr, ec, messages)

	assert.ErrorMatch(err, `\[ERRORS_TEST:123\] annotated: wrapped`)
	assert.Equal(errors.Annotated(err), aerr)
	assert.True(errors.IsInvalidTypeError(errors.Annotated(aerr)))
	assert.Length(errors.Stack(err), 2)
}
Example #21
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")
}
Example #22
0
// Test the validation.
func TestValidation(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)

	ec := 1
	messages := errors.Messages{ec: "valid"}
	err := errors.New(ec, messages)
	packageName, fileName, line, lerr := errors.Location(err)

	assert.True(errors.Valid(err))
	assert.Nil(lerr)
	assert.Equal(packageName, "github.com/pellaeon/goas/v3/errors_test")
	assert.Equal(fileName, "errors_test.go")
	assert.Equal(line, 31)
}
Example #23
0
// TestFlagUnflag tests the removal of a flag.
func TestFlagUnflag(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, false)
	scn := scene.Start()

	err := scn.Flag("foo")
	assert.Nil(err)
	err = scn.Unflag("foo")
	assert.Nil(err)
	err = scn.WaitFlagLimited("foo", 50*time.Millisecond)
	assert.True(scene.IsWaitedTooLongError(err))

	err = scn.Stop()
	assert.Nil(err)
}
Example #24
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")
}
Example #25
0
// TestFatalExit tests the call of the fatal exiter after a
// fatal error log.
func TestFatalExit(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	ownLogger := &testLogger{}
	exited := false
	fatalExiter := func() {
		exited = true
	}

	logger.SetLogger(ownLogger)
	logger.SetFatalExiter(fatalExiter)

	logger.Fatalf("fatal")
	assert.Length(ownLogger.logs, 1)
	assert.True(exited)
}
Example #26
0
// Test regular stop of a recovered loop.
func TestStopRecoverings(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	done := false
	count := 0
	l := loop.GoRecoverable(generateSimplePanicBackend(&done, &count), ignorePanics)

	time.Sleep(longDelay)

	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")
}
Example #27
0
// Test error inside a recovered loop.
func TestRecoveringsError(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	done := false
	count := 0
	l := loop.GoRecoverable(generateErrorPanicBackend(&done, &count), catchTimeout)

	time.Sleep(longDelay)

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

	status, _ := l.Error()

	assert.Equal(loop.Stopped, status, "loop is stopped")
}
Example #28
0
// Test logging with an own logger.
func TestOwnLogger(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	ownLogger := &testLogger{[]string{}}

	logger.SetLevel(logger.LevelDebug)
	logger.SetLogger(ownLogger)

	logger.Debugf("Debug.")
	logger.Infof("Info.")
	logger.Warningf("Warning.")
	logger.Errorf("Error.")
	logger.Criticalf("Critical.")

	assert.Length(ownLogger.logs, 5, "Everything logged.")
}
Example #29
0
// Test logging with the syslogger.
func TestSysLogger(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)

	logger.SetLevel(logger.LevelDebug)

	sl, err := logger.NewSysLogger("GOAS")
	assert.Nil(err)
	logger.SetLogger(sl)

	logger.Debugf("Debug.")
	logger.Infof("Info.")
	logger.Warningf("Warning.")
	logger.Errorf("Error.")
	logger.Criticalf("Critical.")
}
Example #30
0
// Test recoverings after panics.
func TestRecoverings(t *testing.T) {
	assert := asserts.NewTestingAssertion(t, true)
	done := false
	count := 0
	l := loop.GoRecoverable(generateSimplePanicBackend(&done, &count), checkRecovering)

	time.Sleep(veryLongDelay)

	assert.ErrorMatch(l.Stop(), "too many panics", "error has to be 'too many panics'")
	assert.True(done, "backend has done")
	assert.Equal(count, 5, "loop has to be restarted 5 times")

	status, _ := l.Error()

	assert.Equal(loop.Stopped, status, "loop is stopped")
}