// SPDX-License-Identifier: Apache-2.0 // Copyright Authors of K9s package model import ( "context" "strconv" "testing" "time" "github.com/derailed/k9s/internal/client" "github.com/derailed/k9s/internal/dao" "github.com/stretchr/testify/assert" ) func TestUpdateLogs(t *testing.T) { size := 100 m := NewLog(client.NewGVR("fred"), makeLogOpts(size), 10*time.Millisecond) m.Init(makeFactory()) v := newMockLogView() m.AddListener(v) c := make(dao.LogChan, 2) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go m.updateLogs(ctx, c) for i := range 2 * size { c <- dao.NewLogItemFromString("line" + strconv.Itoa(i)) } time.Sleep(2 * time.Second) assert.Equal(t, size, v.count) } func BenchmarkUpdateLogs(b *testing.B) { size := 100 m := NewLog(client.NewGVR("fred"), makeLogOpts(size), 10*time.Millisecond) m.Init(makeFactory()) v := newMockLogView() m.AddListener(v) c := make(dao.LogChan) go func() { m.updateLogs(context.Background(), c) }() item := dao.NewLogItem([]byte("\033[0;38m2018-12-14T10:36:43.326972-07:00 \033[0;32mblee line")) b.ReportAllocs() b.ResetTimer() for range b.N { c <- item } close(c) } // Helpers... func makeLogOpts(count int) *dao.LogOptions { return &dao.LogOptions{ Path: "fred", Container: "blee", Lines: int64(count), } } type mockLogView struct { count int } func newMockLogView() *mockLogView { return &mockLogView{} } func (t *mockLogView) LogChanged(ll [][]byte) { t.count += len(ll) } func (*mockLogView) LogStop() {} func (*mockLogView) LogCanceled() {} func (*mockLogView) LogResume() {} func (*mockLogView) LogCleared() {} func (*mockLogView) LogFailed(error) {}