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
96 lines (78 loc) · 2.22 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
95
96
package testEnv
import (
"fmt"
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/edge"
"github.com/ronaksoft/rony/edgetest"
"github.com/ronaksoft/rony/internal/gateway"
"github.com/ronaksoft/rony/internal/log"
"github.com/ronaksoft/rony/pools"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"sync/atomic"
"time"
)
/*
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 rony.Conn, kvs ...*rony.KeyValue) {
}
func (t testDispatcher) OnClose(conn rony.Conn) {
}
func (t testDispatcher) OnMessage(ctx *edge.DispatchCtx, envelope *rony.MessageEnvelope) {
if ctx.Conn() != nil {
mo := proto.MarshalOptions{
UseCachedSize: true,
}
buf := pools.Buffer.GetCap(mo.Size(envelope))
b, _ := mo.MarshalAppend(*buf.Bytes(), envelope)
err := ctx.Conn().SendBinary(ctx.StreamID(), b)
if err != nil {
log.Warn("Error On SendBinary", zap.Error(err))
}
pools.Buffer.Put(buf)
}
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 InitEdgeServer(serverID string, listenPort int, concurrency int, opts ...edge.Option) *edge.Server {
opts = append(opts,
edge.WithDispatcher(&testDispatcher{}),
edge.WithTcpGateway(edge.TcpGatewayConfig{
Concurrency: concurrency,
ListenAddress: fmt.Sprintf(":%d", listenPort),
MaxIdleTime: time.Second,
Protocol: gateway.TCP,
ExternalAddrs: []string{fmt.Sprintf("127.0.0.1:%d", listenPort)},
}),
)
edgeServer := edge.NewServer(serverID, 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)
}