forked from hazelcast/hazelcast-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_internals_it_test.go
147 lines (135 loc) · 5.11 KB
/
client_internals_it_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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// +build hazelcastinternal
/*
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hazelcast_test
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
hz "github.com/hazelcast/hazelcast-go-client"
"github.com/hazelcast/hazelcast-go-client/hzerrors"
"github.com/hazelcast/hazelcast-go-client/internal/invocation"
"github.com/hazelcast/hazelcast-go-client/internal/it"
"github.com/hazelcast/hazelcast-go-client/internal/proto/codec"
"github.com/hazelcast/hazelcast-go-client/logger"
)
// Tests that require the hazelcastinternal tag.
func TestListenersAfterClientDisconnected(t *testing.T) {
t.Run("MemberHostname_ClientIP", func(t *testing.T) {
testListenersAfterClientDisconnected(t, "localhost", "127.0.0.1", 46501)
})
t.Run("MemberHostname_ClientHostname", func(t *testing.T) {
testListenersAfterClientDisconnected(t, "localhost", "localhost", 47501)
})
t.Run("MemberIP_ClientIP", func(t *testing.T) {
testListenersAfterClientDisconnected(t, "127.0.0.1", "127.0.0.1", 48501)
})
t.Run("MemberIP_ClientHostname", func(t *testing.T) {
testListenersAfterClientDisconnected(t, "127.0.0.1", "localhost", 49501)
})
}
func TestNotReceivedInvocation(t *testing.T) {
// This test skips sending an invocation to the member in order to simulate lost connection.
// After 5 seconds, a GroupLost event is published to simulate the disconnection.
clientTester(t, func(t *testing.T, smart bool) {
tc := it.StartNewClusterWithOptions("not-received-invocation", 55701, 1)
defer tc.Shutdown()
ctx := context.Background()
config := tc.DefaultConfig()
config.Cluster.Unisocket = !smart
client := it.MustClient(hz.StartNewClientWithConfig(ctx, config))
defer client.Shutdown(ctx)
ci := hz.NewClientInternal(client)
var cnt int32
handler := newRiggedInvocationHandler(ci.InvocationHandler(), func(inv invocation.Invocation) bool {
if inv.Request().Type() == codec.MapSetCodecRequestMessageType {
return atomic.AddInt32(&cnt, 1) != 1
}
return true
})
ci.InvocationService().SetHandler(handler)
m := it.MustValue(client.GetMap(ctx, "test-map")).(*hz.Map)
go func() {
time.Sleep(5 * time.Second)
conns := ci.ConnectionManager().ActiveConnections()
ci.DispatchService().Publish(invocation.NewGroupLost(conns[0].ConnectionID(), fmt.Errorf("foo error: %w", hzerrors.ErrIO)))
}()
// the invocation should be sent after 5 seconds, make sure the set operation below succeeds in 10 seconds or times out.
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
err := m.Set(ctx, "foo", "bar")
if err != nil {
t.Fatal(err)
}
})
}
func testListenersAfterClientDisconnected(t *testing.T, memberHost string, clientHost string, port int) {
const heartBeatSec = 6
// launch the cluster
memberConfig := listenersAfterClientDisconnectedXMLConfig(t.Name(), memberHost, port, heartBeatSec)
tc := it.StartNewClusterWithConfig(1, memberConfig, port)
// create and start the client
config := tc.DefaultConfig()
config.Cluster.Network.SetAddresses(fmt.Sprintf("%s:%d", clientHost, port))
if it.TraceLoggingEnabled() {
config.Logger.Level = logger.TraceLevel
}
ctx := context.Background()
client := it.MustClient(hz.StartNewClientWithConfig(ctx, config))
defer client.Shutdown(ctx)
ec := int64(0)
m := it.MustValue(client.GetMap(ctx, it.NewUniqueObjectName("map"))).(*hz.Map)
lc := hz.MapEntryListenerConfig{}
lc.NotifyEntryAdded(true)
it.MustValue(m.AddEntryListener(ctx, lc, func(event *hz.EntryNotified) {
atomic.AddInt64(&ec, 1)
}))
ci := hz.NewClientInternal(client)
// make sure the client connected to the member
it.Eventually(t, func() bool {
ac := len(ci.ConnectionManager().ActiveConnections())
t.Logf("active connections: %d", ac)
return ac == 1
})
// shutdown the member
tc.Shutdown()
time.Sleep(2 * heartBeatSec * time.Second)
// launch a member with the same address
tc = it.StartNewClusterWithConfig(1, memberConfig, port)
defer tc.Shutdown()
// entry notified event handler should run
it.Eventually(t, func() bool {
it.MustValue(m.Remove(ctx, 1))
it.MustValue(m.Put(ctx, 1, 2))
return atomic.LoadInt64(&ec) > 0
})
}
type invokeFilter func(inv invocation.Invocation) (ok bool)
type riggedInvocationHandler struct {
invocation.Handler
invokeFilter invokeFilter
}
func newRiggedInvocationHandler(handler invocation.Handler, filter invokeFilter) *riggedInvocationHandler {
return &riggedInvocationHandler{Handler: handler, invokeFilter: filter}
}
func (h *riggedInvocationHandler) Invoke(inv invocation.Invocation) (int64, error) {
if !h.invokeFilter(inv) {
return 1, nil
}
return h.Handler.Invoke(inv)
}