// 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, `\[E.*\] 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") }) }
// 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.") }
// 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, `\[E.*\] monitor backend panicked`, "monitor restarted due to panic") }
// 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") }
// 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) fileName, line, lerr := errors.Location(err) assert.True(errors.Valid(err)) assert.Nil(lerr) assert.Equal(fileName, "errors_test.go") assert.Equal(line, 31) }
// 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) }
// 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") }
// 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/tideland/goas/v3/errors_test") assert.Equal(fileName, "errors_test.go") assert.Equal(line, 31) }
// 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") }
// 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.") }
// 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.") }
// 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") }
// 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") }
// 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") }
// Test creation and checking. func TestIsError(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) ec := 42 messages := errors.Messages{ec: "test error %d"} err := errors.New(ec, messages, 1) assert.ErrorMatch(err, `\[ERRORS_TEST:042\] test error 1`) assert.True(errors.IsError(err, ec)) assert.False(errors.IsError(err, 0)) err = testError("test error 2") assert.ErrorMatch(err, "test error 2") assert.False(errors.IsError(err, ec)) assert.False(errors.IsError(err, 0)) }
// Test of the DSR monitor. func TestDsrMonitor(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) // Register monitoring funcs. monitoring.Register("dsr:a", func() (string, error) { return "A", nil }) monitoring.Register("dsr:b", func() (string, error) { return "4711", nil }) monitoring.Register("dsr:c", func() (string, error) { return "2012-02-15", nil }) monitoring.Register("dsr:d", func() (string, error) { a := 1; a = a / (a - a); return fmt.Sprintf("%d", a), nil }) // Need some time to let that backend catch up queued registerings. time.Sleep(time.Millisecond) // Asserts. dsv, err := monitoring.ReadStatus("foo") assert.ErrorMatch(err, `\[E.*\] dynamic status "foo" does not exist`, "reading non-existent status") dsv, err = monitoring.ReadStatus("dsr:b") assert.Nil(err, "no error expected") assert.Equal(dsv, "4711", "status value should be correct") dsv, err = monitoring.ReadStatus("dsr:d") assert.NotNil(err, "error should be returned") assert.ErrorMatch(err, `\[E.*\] monitor backend panicked`, "error inside retrieval has to be catched") }
// Test of the SSI monitor. func TestSsiMonitor(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) // Generate values. for i := 0; i < 500; i++ { n := rand.Intn(10) id := fmt.Sprintf("ssv:value:%d", n) monitoring.SetVariable(id, rand.Int63n(2001)-1000) } // Need some time to let that backend catch up queued mesurings. time.Sleep(time.Millisecond) // Asserts. ssv, err := monitoring.ReadVariable("foo") assert.ErrorMatch(err, `\[E.*\] stay-set variable "foo" does not exist`, "reading non-existent variable") ssv, err = monitoring.ReadVariable("ssv:value:5") assert.Nil(err, "no error expected") assert.Equal(ssv.Id, "ssv:value:5", "should get the right one") assert.True(ssv.Count > 0, "should be set several times") assert.Match(ssv.String(), `Stay-Set Variable "ssv:value:5" (.*)`, "string representation should look fine") monitoring.StaySetVariablesDo(func(ssv *monitoring.StaySetVariable) { assert.Match(ssv.Id, "ssv:value:[0-9]", "id has to match the pattern") assert.True(ssv.MinValue <= ssv.AvgValue && ssv.AvgValue <= ssv.MaxValue, "avg should be somewhere between min and max") }) }
// TestLess tests the comparing of two versions. func TestLess(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) tests := []struct { vsnA version.Version vsnB version.Version less bool }{ { vsnA: version.New(1, 2, 3), vsnB: version.New(1, 2, 3), less: false, }, { vsnA: version.New(1, 2, 3), vsnB: version.New(1, 2, 4), less: true, }, { vsnA: version.New(1, 2, 3), vsnB: version.New(1, 3, 3), less: true, }, { vsnA: version.New(1, 2, 3), vsnB: version.New(2, 2, 3), less: true, }, { vsnA: version.New(3, 2, 1), vsnB: version.New(1, 2, 3), less: false, }, { vsnA: version.New(1, 2, 3, "alpha"), vsnB: version.New(1, 2, 3), less: true, }, { vsnA: version.New(1, 2, 3, "alpha", "1"), vsnB: version.New(1, 2, 3, "alpha"), less: true, }, { vsnA: version.New(1, 2, 3, "alpha", "1"), vsnB: version.New(1, 2, 3, "alpha", "2"), less: true, }, { vsnA: version.New(1, 2, 3, "alpha", "4711"), vsnB: version.New(1, 2, 3, "alpha", "471"), less: false, }, { vsnA: version.New(1, 2, 3, "alpha", "48"), vsnB: version.New(1, 2, 3, "alpha", "4711"), less: true, }, { vsnA: version.New(1, 2, 3, version.Metadata, "alpha", "1"), vsnB: version.New(1, 2, 3, version.Metadata, "alpha", "2"), less: false, }, { vsnA: version.New(1, 2, 3, version.Metadata, "alpha", "2"), vsnB: version.New(1, 2, 3, version.Metadata, "alpha", "1"), less: false, }, { vsnA: version.New(1, 2, 3, "alpha", version.Metadata, "alpha", "2"), vsnB: version.New(1, 2, 3, "alpha", version.Metadata, "alpha", "1"), less: false, }, { vsnA: version.New(1, 2, 3, "alpha", "48", version.Metadata, "alpha", "2"), vsnB: version.New(1, 2, 3, "alpha", "4711", version.Metadata, "alpha", "1"), less: true, }, { vsnA: version.New(1, 2, 3, "alpha", "2"), vsnB: version.New(1, 2, 3, "alpha", "1b"), less: false, }, } for i, test := range tests { assert.Logf("test #%d: %q < %q", i, test.vsnA, test.vsnB) assert.Equal(test.vsnA.Less(test.vsnB), test.less) } }
// TestVersion tests the creation of a new versions and their // accessor ethods. func TestVersion(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) tests := []struct { id string vsn version.Version major int minor int patch int preRelease string metadata string }{ { id: "v1.2.3", vsn: version.New(1, 2, 3), major: 1, minor: 2, patch: 3, preRelease: "", metadata: "", }, { id: "v1.0.3", vsn: version.New(1, -2, 3), major: 1, minor: 0, patch: 3, preRelease: "", metadata: "", }, { id: "v1.2.3-alpha.2014-08-03", vsn: version.New(1, 2, 3, "alpha", "2014-08-03"), major: 1, minor: 2, patch: 3, preRelease: "alpha.2014-08-03", metadata: "", }, { id: "v1.2.3-alphabeta.7.11", vsn: version.New(1, 2, 3, "alpha beta", "007", "1+1"), major: 1, minor: 2, patch: 3, preRelease: "alphabeta.7.11", metadata: "", }, { id: "v1.2.3+007.a", vsn: version.New(1, 2, 3, version.Metadata, "007", "a"), major: 1, minor: 2, patch: 3, preRelease: "", metadata: "007.a", }, { id: "v1.2.3-alpha+007.a", vsn: version.New(1, 2, 3, "alpha", version.Metadata, "007", "a"), major: 1, minor: 2, patch: 3, preRelease: "alpha", metadata: "007.a", }, { id: "v1.2.3-ALPHA+007.a", vsn: version.New(1, 2, 3, "ALPHA", version.Metadata, "007", "a"), major: 1, minor: 2, patch: 3, preRelease: "ALPHA", metadata: "007.a", }, } for i, test := range tests { assert.Logf("test #%d: %q", i, test.id) assert.Equal(test.vsn.Major(), test.major) assert.Equal(test.vsn.Minor(), test.minor) assert.Equal(test.vsn.Patch(), test.patch) assert.Equal(test.vsn.PreRelease(), test.preRelease) assert.Equal(test.vsn.Metadata(), test.metadata) assert.Equal(test.vsn.String(), test.id) } }