-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubscription_test.go
More file actions
107 lines (86 loc) · 2.4 KB
/
subscription_test.go
File metadata and controls
107 lines (86 loc) · 2.4 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package stride
import (
"context"
"fmt"
"net"
"net/http"
"sync/atomic"
"testing"
"time"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type SubscriptionTestSuite struct {
suite.Suite
}
func createMockSubscribeServer(stop chan bool, delay time.Duration, count int) (*echo.Echo, string) {
e := echo.New()
written := 0
e.GET("v1/collect/stream/subscribe", func(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
c.Response().Flush()
for written < count {
c.Response().Write([]byte(`{"ts": "2016-10-03T22:19:51Z", "user": "cartman"}`))
c.Response().Write([]byte(delimiter))
c.Response().Flush()
written++
select {
case <-stop:
return nil
case <-time.After(delay):
}
}
// Stay running, the client will determine when to close the connection
for {
time.Sleep(1 * time.Second)
}
})
l, _ := net.Listen("tcp", "localhost:0")
addr := l.Addr().String()
l.Close()
go func() { e.Start(addr) }()
// Wait for server to boot up
time.Sleep(2 * time.Second)
return e, addr
}
func (suite *SubscriptionTestSuite) TestSubscription() {
stop := make(chan bool)
e, addr := createMockSubscribeServer(stop, 1*time.Millisecond, 10000)
defer func() {
cxt, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
e.Shutdown(cxt)
}()
config := NewConfig()
config.Endpoint = fmt.Sprintf("http://%s/v1", addr)
s := newSubscription("key", "/collect/stream", config)
s.Start()
start := time.Now()
assert.True(suite.T(), s.IsRunning())
var count int32
atomic.StoreInt32(&count, 0)
t := suite.T()
go func() {
for event := range s.Events {
atomic.AddInt32(&count, 1)
assert.Equal(t, "cartman", event["user"])
assert.Equal(t, "2016-10-03T22:19:51Z", event["ts"])
}
}()
for atomic.LoadInt32(&count) < 10000 && time.Since(start) < 20*time.Second {
time.Sleep(100 * time.Millisecond)
}
assert.True(suite.T(), s.IsConnected())
assert.Equal(suite.T(), int32(10000), atomic.LoadInt32(&count))
close(stop)
assert.Equal(suite.T(), int32(10000), atomic.LoadInt32(&count))
err := s.Stop()
assert.Nil(suite.T(), err)
assert.False(suite.T(), s.IsConnected())
assert.False(suite.T(), s.IsRunning())
}
func TestSubscriptionTestSuite(t *testing.T) {
suite.Run(t, new(SubscriptionTestSuite))
}