github.com/drone/runner-go@v1.12.0/livelog/livelog_test.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package livelog 6 7 import ( 8 "context" 9 "testing" 10 "time" 11 12 "github.com/drone/drone-go/drone" 13 "github.com/drone/runner-go/client" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestLineWriterSingle(t *testing.T) { 19 client := new(mockClient) 20 w := New(client, 1) 21 w.SetInterval(time.Duration(0)) 22 w.num = 4 23 w.Write([]byte("foo\nbar\n")) 24 25 a := w.pending 26 b := []*drone.Line{ 27 {Number: 4, Message: "foo\n"}, 28 {Number: 5, Message: "bar\n"}, 29 {Number: 6, Message: ""}, 30 } 31 if diff := cmp.Diff(a, b); diff != "" { 32 t.Fail() 33 t.Log(diff) 34 } 35 36 w.Close() 37 a = client.uploaded 38 if diff := cmp.Diff(a, b); diff != "" { 39 t.Fail() 40 t.Log(diff) 41 } 42 43 if len(w.pending) > 0 { 44 t.Errorf("Expect empty buffer") 45 } 46 } 47 48 func TestLineWriterLimit(t *testing.T) { 49 client := new(mockClient) 50 w := New(client, 0) 51 if got, want := w.limit, defaultLimit; got != want { 52 t.Errorf("Expect default buffer limit %d, got %d", want, got) 53 } 54 w.SetLimit(6) 55 if got, want := w.limit, 6; got != want { 56 t.Errorf("Expect custom buffer limit %d, got %d", want, got) 57 } 58 59 w.Write([]byte("foo")) 60 w.Write([]byte("bar")) 61 w.Write([]byte("baz")) 62 63 if got, want := w.size, 6; got != want { 64 t.Errorf("Expect buffer size %d, got %d", want, got) 65 } 66 67 a := w.history 68 b := []*drone.Line{ 69 {Number: 1, Message: "bar"}, 70 {Number: 2, Message: "baz"}, 71 } 72 if diff := cmp.Diff(a, b); diff != "" { 73 t.Fail() 74 t.Log(diff) 75 } 76 } 77 78 type mockClient struct { 79 client.Client 80 lines []*drone.Line 81 uploaded []*drone.Line 82 } 83 84 func (m *mockClient) Batch(ctx context.Context, id int64, lines []*drone.Line) error { 85 m.lines = append(m.lines, lines...) 86 return nil 87 } 88 89 func (m *mockClient) Upload(ctx context.Context, id int64, lines []*drone.Line) error { 90 m.uploaded = lines 91 return nil 92 }