MCPcopy
hub / github.com/uber-go/zap / TestBufferWriter

Function TestBufferWriter

zapcore/buffered_write_syncer_test.go:33–136  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

31)
32
33func TestBufferWriter(t *testing.T) {
34 // If we pass a plain io.Writer, make sure that we still get a WriteSyncer
35 // with a no-op Sync.
36 t.Run("sync", func(t *testing.T) {
37 buf := &bytes.Buffer{}
38 ws := &BufferedWriteSyncer{WS: AddSync(buf)}
39
40 requireWriteWorks(t, ws)
41 assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
42 assert.NoError(t, ws.Sync(), "Unexpected error calling a no-op Sync method.")
43 assert.Equal(t, "foo", buf.String(), "Unexpected log string")
44 assert.NoError(t, ws.Stop())
45 })
46
47 t.Run("stop", func(t *testing.T) {
48 buf := &bytes.Buffer{}
49 ws := &BufferedWriteSyncer{WS: AddSync(buf)}
50 requireWriteWorks(t, ws)
51 assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
52 assert.NoError(t, ws.Stop())
53 assert.Equal(t, "foo", buf.String(), "Unexpected log string")
54 })
55
56 t.Run("stop race with flush", func(t *testing.T) {
57 buf := &bytes.Buffer{}
58 ws := &BufferedWriteSyncer{WS: AddSync(buf), FlushInterval: 1}
59 requireWriteWorks(t, ws)
60 assert.NoError(t, ws.Stop())
61 assert.Equal(t, "foo", buf.String(), "Unexpected log string")
62 })
63
64 t.Run("stop twice", func(t *testing.T) {
65 ws := &BufferedWriteSyncer{WS: &ztest.FailWriter{}}
66 _, err := ws.Write([]byte("foo"))
67 require.NoError(t, err, "Unexpected error writing to WriteSyncer.")
68 assert.Error(t, ws.Stop(), "Expected stop to fail.")
69 assert.NoError(t, ws.Stop(), "Expected stop to not fail.")
70 })
71
72 t.Run("wrap twice", func(t *testing.T) {
73 buf := &bytes.Buffer{}
74 bufsync := &BufferedWriteSyncer{WS: AddSync(buf)}
75 ws := &BufferedWriteSyncer{WS: bufsync}
76 requireWriteWorks(t, ws)
77 assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
78 require.NoError(t, ws.Sync())
79 assert.Equal(t, "foo", buf.String())
80 assert.NoError(t, ws.Stop())
81 assert.NoError(t, bufsync.Stop())
82 assert.Equal(t, "foo", buf.String(), "Unexpected log string")
83 })
84
85 t.Run("small buffer", func(t *testing.T) {
86 buf := &bytes.Buffer{}
87 ws := &BufferedWriteSyncer{WS: AddSync(buf), Size: 5}
88
89 requireWriteWorks(t, ws)
90 assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.")

Callers

nothing calls this directly

Calls 10

StringMethod · 0.95
SyncMethod · 0.95
StopMethod · 0.95
WriteMethod · 0.95
AddMethod · 0.95
NewMockClockFunction · 0.92
AddSyncFunction · 0.85
requireWriteWorksFunction · 0.85
LockFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected