This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_server.go
94 lines (76 loc) · 2.14 KB
/
test_server.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
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
package testEnv
import (
"fmt"
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/edge"
"github.com/ronaksoft/rony/edgetest"
"github.com/ronaksoft/rony/gateway"
tcpGateway "github.com/ronaksoft/rony/gateway/tcp"
log "github.com/ronaksoft/rony/internal/logger"
"github.com/ronaksoft/rony/pools"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"sync/atomic"
)
/*
Creation Time: 2020 - Apr - 10
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
var (
receivedMessages int32
receivedUpdates int32
)
type testDispatcher struct {
}
func (t testDispatcher) OnOpen(conn gateway.Conn, kvs ...gateway.KeyValue) {
}
func (t testDispatcher) OnClose(conn gateway.Conn) {
}
func (t testDispatcher) OnMessage(ctx *edge.DispatchCtx, envelope *rony.MessageEnvelope) {
if ctx.Conn() != nil {
mo := proto.MarshalOptions{
UseCachedSize: true,
}
b := pools.Bytes.GetCap(mo.Size(envelope))
b, _ = mo.MarshalAppend(b, envelope)
err := ctx.Conn().SendBinary(ctx.StreamID(), b)
if err != nil {
log.Warn("Error On SendBinary", zap.Error(err))
}
pools.Bytes.Put(b)
}
atomic.AddInt32(&receivedMessages, 1)
}
func (t testDispatcher) Interceptor(ctx *edge.DispatchCtx, data []byte) (err error) {
return ctx.UnmarshalEnvelope(data)
}
func (t testDispatcher) Done(ctx *edge.DispatchCtx) {}
func InitEdgeServerWithWebsocket(serverID string, clientPort int, opts ...edge.Option) *edge.Server {
opts = append(opts,
edge.WithTcpGateway(tcpGateway.Config{
Protocol: tcpGateway.Websocket,
Concurrency: 10,
MaxIdleTime: 0,
ListenAddress: fmt.Sprintf(":%d", clientPort),
}),
)
edgeServer := edge.NewServer(serverID, &testDispatcher{}, opts...)
return edgeServer
}
func InitTestServer(serverID string) *edgetest.Server {
return edgetest.NewServer(serverID, &testDispatcher{})
}
func ResetCounters() {
atomic.StoreInt32(&receivedMessages, 0)
atomic.StoreInt32(&receivedUpdates, 0)
}
func ReceivedMessages() int32 {
return atomic.LoadInt32(&receivedMessages)
}
func ReceivedUpdates() int32 {
return atomic.LoadInt32(&receivedUpdates)
}