-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 100% coverage test for cancelableReader
- Loading branch information
1 parent
a7efd3f
commit de8493d
Showing
1 changed file
with
63 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package extract | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCancelableReader(t *testing.T) { | ||
var b [100000]byte | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
reader := newCancelableReader(ctx, bytes.NewReader(b[:])) | ||
defer cancel() | ||
|
||
var buff [1000]byte | ||
readed := 0 | ||
for { | ||
n, err := reader.Read(buff[:]) | ||
if err != nil { | ||
fmt.Println("exit error:", err) | ||
require.Equal(t, "EOF", err.Error()) | ||
break | ||
} | ||
require.NotZero(t, n) | ||
time.Sleep(10 * time.Millisecond) | ||
readed += n | ||
} | ||
|
||
fmt.Println("Readed", readed, "out of", len(b)) | ||
require.Equal(t, len(b), readed) | ||
} | ||
|
||
func TestCancelableReaderWithInterruption(t *testing.T) { | ||
var b [100000]byte | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
reader := newCancelableReader(ctx, bytes.NewReader(b[:])) | ||
defer cancel() | ||
|
||
go func() { | ||
time.Sleep(100 * time.Millisecond) | ||
cancel() | ||
}() | ||
|
||
var buff [1000]byte | ||
readed := 0 | ||
for { | ||
n, err := reader.Read(buff[:]) | ||
if err != nil { | ||
fmt.Println("exit error:", err) | ||
require.Equal(t, "interrupted", err.Error()) | ||
break | ||
} | ||
require.NotZero(t, n) | ||
time.Sleep(10 * time.Millisecond) | ||
readed += n | ||
} | ||
fmt.Println("Readed", readed, "out of", len(b)) | ||
require.True(t, readed < len(b)) | ||
} |