-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File Stream - Support multi-writers (#58)
- Loading branch information
Showing
7 changed files
with
115 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
var ErrShortWrite = errors.New("The number of bytes written is less than the length of the input") | ||
|
||
type asyncMultiWriter struct { | ||
writers []io.Writer | ||
limit int | ||
} | ||
|
||
// AsyncMultiWriter creates a writer that duplicates its writes to all the | ||
// provided writers asynchronous | ||
func AsyncMultiWriter(limit int, writers ...io.Writer) io.Writer { | ||
w := make([]io.Writer, len(writers)) | ||
copy(w, writers) | ||
return &asyncMultiWriter{writers: w, limit: limit} | ||
} | ||
|
||
// Writes data asynchronously to each writer and waits for all of them to complete. | ||
// In case of an error, the writing will not complete. | ||
func (amw *asyncMultiWriter) Write(p []byte) (int, error) { | ||
eg := errgroup.Group{} | ||
eg.SetLimit(amw.limit) | ||
for _, w := range amw.writers { | ||
currentWriter := w | ||
eg.Go(func() error { | ||
n, err := currentWriter.Write(p) | ||
if err != nil { | ||
return err | ||
} | ||
if n != len(p) { | ||
return ErrShortWrite | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
return len(p), eg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAsyncMultiWriter(t *testing.T) { | ||
for _, limit := range []int{1, 2} { | ||
var buf1, buf2 bytes.Buffer | ||
multiWriter := AsyncMultiWriter(limit, &buf1, &buf2) | ||
|
||
data := []byte("test data") | ||
n, err := multiWriter.Write(data) | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(data), n) | ||
|
||
// Check if data is correctly written to both writers | ||
assert.Equal(t, string(data), buf1.String()) | ||
assert.Equal(t, string(data), buf2.String()) | ||
} | ||
} | ||
|
||
func TestAsyncMultiWriter_Error(t *testing.T) { | ||
expectedErr := errors.New("write error") | ||
|
||
// Mock writer that always returns an error | ||
mockWriter := &mockWriter{writeErr: expectedErr} | ||
multiWriter := AsyncMultiWriter(2, mockWriter) | ||
|
||
_, err := multiWriter.Write([]byte("test data")) | ||
assert.Equal(t, expectedErr, err) | ||
} | ||
|
||
// Mock writer to simulate Write errors | ||
type mockWriter struct { | ||
writeErr error | ||
} | ||
|
||
func (m *mockWriter) Write(p []byte) (int, error) { | ||
return 0, m.writeErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters