Skip to content

Commit

Permalink
Added 100% coverage test for cancelableReader
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie authored and matteosuppo committed Nov 6, 2018
1 parent a7efd3f commit de8493d
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions cancelable_reader_test.go
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))
}

0 comments on commit de8493d

Please sign in to comment.