Selaa lähdekoodia

Add primitive relay tests

tags/v2.0.0-rc1
9seconds 5 vuotta sitten
vanhempi
commit
6b1bfe7b17

+ 49
- 0
mtglib/internal/relay/init_test.go Näytä tiedosto

@@ -0,0 +1,49 @@
1
+package relay_test
2
+
3
+import (
4
+	"bytes"
5
+	"io"
6
+	"sync"
7
+)
8
+
9
+type loggerMock struct{}
10
+
11
+func (l loggerMock) Printf(format string, args ...interface{}) {}
12
+
13
+type rwcMock struct {
14
+	bytes.Buffer
15
+
16
+	closed bool
17
+	mutex  sync.Mutex
18
+}
19
+
20
+func (r *rwcMock) Read(p []byte) (int, error) {
21
+	r.mutex.Lock()
22
+	defer r.mutex.Unlock()
23
+
24
+	if r.closed {
25
+		return 0, io.EOF
26
+	}
27
+
28
+	return r.Buffer.Read(p)
29
+}
30
+
31
+func (r *rwcMock) Write(p []byte) (int, error) {
32
+	r.mutex.Lock()
33
+	defer r.mutex.Unlock()
34
+
35
+	if r.closed {
36
+		return 0, io.EOF
37
+	}
38
+
39
+	return r.Buffer.Write(p)
40
+}
41
+
42
+func (r *rwcMock) Close() error {
43
+	r.mutex.Lock()
44
+	defer r.mutex.Unlock()
45
+
46
+	r.closed = true
47
+
48
+	return nil
49
+}

+ 8
- 4
mtglib/internal/relay/relay.go Näytä tiedosto

@@ -34,7 +34,7 @@ func (r *Relay) Process(eastConn, westConn io.ReadWriteCloser) error {
34 34
 		westConn.Close()
35 35
 	}()
36 36
 
37
-	go r.runObserver()
37
+	go r.runObserver(r.ctx)
38 38
 
39 39
 	wg := &sync.WaitGroup{}
40 40
 	wg.Add(2) // nolint: gomnd
@@ -45,7 +45,12 @@ func (r *Relay) Process(eastConn, westConn io.ReadWriteCloser) error {
45 45
 
46 46
 	wg.Wait()
47 47
 
48
-	return <-r.errorChannel
48
+	select {
49
+	case err := <-r.errorChannel:
50
+		return err
51
+	default:
52
+		return nil
53
+	}
49 54
 }
50 55
 
51 56
 func (r *Relay) transmit(src io.ReadCloser, dst io.WriteCloser,
@@ -67,7 +72,7 @@ func (r *Relay) transmit(src io.ReadCloser, dst io.WriteCloser,
67 72
 	}
68 73
 }
69 74
 
70
-func (r *Relay) runObserver() {
75
+func (r *Relay) runObserver(ctx context.Context) {
71 76
 	ticker := time.NewTicker(time.Second)
72 77
 
73 78
 	defer func() {
@@ -80,7 +85,6 @@ func (r *Relay) runObserver() {
80 85
 	}()
81 86
 
82 87
 	lastTickAt := time.Now()
83
-	ctx := r.ctx
84 88
 
85 89
 	for {
86 90
 		select {

+ 56
- 0
mtglib/internal/relay/relay_test.go Näytä tiedosto

@@ -0,0 +1,56 @@
1
+package relay_test
2
+
3
+import (
4
+	"context"
5
+	"testing"
6
+	"time"
7
+
8
+	"github.com/9seconds/mtg/v2/mtglib/internal/relay"
9
+	"github.com/stretchr/testify/suite"
10
+)
11
+
12
+type RelayTestSuite struct {
13
+	suite.Suite
14
+
15
+	ctx       context.Context
16
+	ctxCancel context.CancelFunc
17
+	r         *relay.Relay
18
+}
19
+
20
+func (suite *RelayTestSuite) SetupTest() {
21
+	suite.ctx, suite.ctxCancel = context.WithCancel(context.Background())
22
+	suite.r = relay.AcquireRelay(suite.ctx, loggerMock{}, 4096, time.Second)
23
+}
24
+
25
+func (suite *RelayTestSuite) TearDownTest() {
26
+	suite.ctxCancel()
27
+	relay.ReleaseRelay(suite.r)
28
+	suite.r = nil
29
+}
30
+
31
+func (suite *RelayTestSuite) TestCancelled() {
32
+	suite.ctxCancel()
33
+
34
+	eastConn := &rwcMock{}
35
+	eastConn.Write([]byte{1, 2, 3, 4, 5})
36
+
37
+	westConn := &rwcMock{}
38
+	westConn.Write([]byte{100, 101, 102})
39
+
40
+	suite.Nil(suite.r.Process(eastConn, westConn))
41
+}
42
+
43
+func (suite *RelayTestSuite) TestCopyFine() {
44
+	eastConn := &rwcMock{}
45
+	eastConn.Write([]byte{1, 2, 3, 4, 5})
46
+
47
+	westConn := &rwcMock{}
48
+	westConn.Write([]byte{100, 101, 102})
49
+
50
+	suite.NotNil(suite.r.Process(eastConn, westConn))
51
+}
52
+
53
+func TestRelay(t *testing.T) {
54
+	t.Parallel()
55
+	suite.Run(t, &RelayTestSuite{})
56
+}

Loading…
Peruuta
Tallenna