2019-12-17 21:16:45 +03:00
|
|
|
package memory
|
2019-11-27 19:02:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2019-12-17 21:16:45 +03:00
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/debug/log"
|
2019-11-27 19:02:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogger(t *testing.T) {
|
2019-11-27 20:31:35 +03:00
|
|
|
// set size to some value
|
|
|
|
size := 100
|
|
|
|
// override the global logger
|
2019-12-17 21:16:45 +03:00
|
|
|
lg := NewLog(log.Size(size))
|
2019-11-27 19:02:16 +03:00
|
|
|
// make sure we have the right size of the logger ring buffer
|
2019-12-17 21:16:45 +03:00
|
|
|
if lg.(*memoryLog).Size() != size {
|
|
|
|
t.Errorf("expected buffer size: %d, got: %d", size, lg.(*memoryLog).Size())
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log some cruft
|
2019-12-18 19:02:11 +03:00
|
|
|
lg.Write(log.Record{Message: "foobar"})
|
|
|
|
lg.Write(log.Record{Message: "foo bar"})
|
2019-11-27 19:02:16 +03:00
|
|
|
|
|
|
|
// Check if the logs are stored in the logger ring buffer
|
2019-11-27 20:31:35 +03:00
|
|
|
expected := []string{"foobar", "foo bar"}
|
2019-12-17 21:16:45 +03:00
|
|
|
entries, _ := lg.Read(log.Count(len(expected)))
|
2019-11-27 19:02:16 +03:00
|
|
|
for i, entry := range entries {
|
2019-12-18 19:02:11 +03:00
|
|
|
if !reflect.DeepEqual(entry.Message, expected[i]) {
|
|
|
|
t.Errorf("expected %s, got %s", expected[i], entry.Message)
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|