-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package minimp3 | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// TestIssue18DelayPutData ... | ||
func TestIssue18DelayPutData(t *testing.T) { | ||
reader, writer := io.Pipe() | ||
dec, err := NewDecoder(reader) | ||
if err != nil { | ||
t.Errorf("NewDecoder failed: %v", err) | ||
} | ||
go func() { | ||
time.Sleep(3 * time.Second) | ||
file, err := os.ReadFile("test.mp3") | ||
if err != nil { | ||
t.Errorf("open file failed: %v", err) | ||
} | ||
_, err = io.Copy(writer, bytes.NewReader(file)) | ||
if err != nil { | ||
t.Errorf("copy mp3 data to pipe failed: %v", err) | ||
} | ||
writer.Close() // nolint: errcheck | ||
}() | ||
// seems like the 'dec.Started' is unnecessary here | ||
data, err := io.ReadAll(dec) | ||
if err != nil { | ||
t.Errorf("read the whole decoded data failed: %v", err) | ||
} | ||
if len(data) != 44928 { | ||
t.Errorf("decode mp3 file failed, real is 44928, but got %d", len(data)) | ||
} | ||
} | ||
|
||
// TestIssue18GracefulExit ... | ||
func TestIssue18GracefulExit(t *testing.T) { | ||
reader, writer := io.Pipe() | ||
dec, err := NewDecoder(reader) | ||
if err != nil { | ||
t.Errorf("NewDecoder failed: %v", err) | ||
} | ||
go func() { | ||
time.Sleep(3 * time.Second) | ||
writer.Close() // nolint: errcheck | ||
}() | ||
// seems like the 'dec.Started' is necessary here | ||
// if the Decoder input reader is closed, then the Decoder.Read will be returned | ||
data, err := io.ReadAll(dec) | ||
if err != nil { | ||
t.Errorf("read the whole decoded data failed: %v", err) | ||
} | ||
if len(data) != 0 { | ||
t.Errorf("graceful exit read something data") | ||
} | ||
} |