Repository: uber-go/zap
File: zapcore/buffered_write_syncer_test.go (Go)

Code before:
		assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
		assert.NoError(t, ws.Sync(), "Unexpected error calling a no-op Sync method.")
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
		assert.NoError(t, ws.Close())
	})

	t.Run("close", func(t *testing.T) {
		buf := &bytes.Buffer{}
		ws := &BufferedWriteSyncer{WriteSyncer: AddSync(buf)}
		requireWriteWorks(t, ws)
		assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
		assert.NoError(t, ws.Close())
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
	})

	t.Run("wrap twice", func(t *testing.T) {
		buf := &bytes.Buffer{}
		bufsync := &BufferedWriteSyncer{WriteSyncer: AddSync(buf)}
		ws := &BufferedWriteSyncer{WriteSyncer: bufsync}
		requireWriteWorks(t, ws)
		assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.")
		assert.NoError(t, ws.Close())
		assert.NoError(t, bufsync.Close())
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
	})

	t.Run("small buffer", func(t *testing.T) {
		buf := &bytes.Buffer{}
		ws := &BufferedWriteSyncer{WriteSyncer: AddSync(buf), Size: 5}

		requireWriteWorks(t, ws)
		assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.")
		requireWriteWorks(t, ws)
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
		assert.NoError(t, ws.Close())
	})

	t.Run("with lockedWriteSyncer", func(t *testing.T) {
		buf := &bytes.Buffer{}
		ws := &BufferedWriteSyncer{WriteSyncer: &lockedWriteSyncer{ws: AddSync(buf)}, Size: 5}

		requireWriteWorks(t, ws)
		assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.")
		requireWriteWorks(t, ws)
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
		assert.NoError(t, ws.Close())
	})

	t.Run("flush error", func(t *testing.T) {
		ws := &BufferedWriteSyncer{WriteSyncer: &ztest.FailWriter{}, Size: 4}
		n, err := ws.Write([]byte("foo"))

Review comment (suggestion):
We can use the wrap twice case to also validate that we call Sync on the underlying writer by calling sync on the outer BufferedWriteSyncer and asserting the contents of the buffer.

Something like:

```
require.NoError(t, ws.Sync())
assert.Equal(t, "foo", buf.String())
```

(Because if we didn't call sync on the underlying writesyncer, then the inner ws wouldn't write to the buffer.)

Code after:
		assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
		assert.NoError(t, ws.Sync(), "Unexpected error calling a no-op Sync method.")
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
		assert.NoError(t, ws.Close())
	})

	t.Run("close", func(t *testing.T) {
		buf := &bytes.Buffer{}
		ws := &BufferedWriteSyncer{WriteSyncer: AddSync(buf)}
		requireWriteWorks(t, ws)
		assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
		assert.NoError(t, ws.Close())
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
	})

	t.Run("wrap twice", func(t *testing.T) {
		buf := &bytes.Buffer{}
		bufsync := &BufferedWriteSyncer{WriteSyncer: AddSync(buf)}
		ws := &BufferedWriteSyncer{WriteSyncer: bufsync}
		requireWriteWorks(t, ws)
		assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
		require.NoError(t, ws.Sync())
		assert.Equal(t, "foo", buf.String())
		assert.NoError(t, ws.Close())
		assert.NoError(t, bufsync.Close())
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
	})

	t.Run("small buffer", func(t *testing.T) {
		buf := &bytes.Buffer{}
		ws := &BufferedWriteSyncer{WriteSyncer: AddSync(buf), Size: 5}

		requireWriteWorks(t, ws)
		assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.")
		requireWriteWorks(t, ws)
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
		assert.NoError(t, ws.Close())
	})

	t.Run("with lockedWriteSyncer", func(t *testing.T) {
		buf := &bytes.Buffer{}
		ws := &BufferedWriteSyncer{WriteSyncer: Lock(AddSync(buf)), Size: 5}

		requireWriteWorks(t, ws)
		assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.")
		requireWriteWorks(t, ws)
		assert.Equal(t, "foo", buf.String(), "Unexpected log string")
		assert.NoError(t, ws.Close())
	})

	t.Run("flush error", func(t *testing.T) {