This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
/
reader_test.go
67 lines (59 loc) · 1.58 KB
/
reader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package astilectron
import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
// mockedReader represents a mocked reader
type mockedReader struct {
*bytes.Buffer
c bool
}
// Close implements the io.Close interface
func (r *mockedReader) Close() error {
r.c = true
return nil
}
func TestReader_IsEOFErr(t *testing.T) {
var r = newReader(context.Background(), &logger{}, &dispatcher{}, ioutil.NopCloser(&bytes.Buffer{}))
assert.True(t, r.isEOFErr(io.EOF))
assert.True(t, r.isEOFErr(errors.New("read tcp 127.0.0.1:56093->127.0.0.1:56092: wsarecv: An existing connection was forcibly closed by the remote host.")))
assert.False(t, r.isEOFErr(errors.New("random error")))
}
func TestReader(t *testing.T) {
// Init
var mr = &mockedReader{Buffer: bytes.NewBuffer([]byte("{\"name\":\"1\",\"targetId\":\"1\"}\n{\n{\"name\":\"2\",\"targetId\":\"2\"}\n"))}
var d = newDispatcher()
var wg = &sync.WaitGroup{}
var dispatched = []int{}
var dispatchedMutex = sync.Mutex{}
d.addListener("1", "1", func(e Event) (deleteListener bool) {
dispatchedMutex.Lock()
dispatched = append(dispatched, 1)
dispatchedMutex.Unlock()
wg.Done()
return
})
d.addListener("2", "2", func(e Event) (deleteListener bool) {
dispatchedMutex.Lock()
dispatched = append(dispatched, 2)
dispatchedMutex.Unlock()
wg.Done()
return
})
wg.Add(2)
var r = newReader(context.Background(), &logger{}, d, mr)
// Test read
go r.read()
wg.Wait()
assert.Contains(t, dispatched, 1)
assert.Contains(t, dispatched, 2)
// Test close
r.close()
assert.True(t, mr.c)
}