-
Notifications
You must be signed in to change notification settings - Fork 58
/
cpmap_it_test.go
169 lines (154 loc) · 4.94 KB
/
cpmap_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2008-2023, 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"
"testing"
"github.com/stretchr/testify/require"
"github.com/hazelcast/hazelcast-go-client/internal/check"
"github.com/hazelcast/hazelcast-go-client/internal/it"
"github.com/hazelcast/hazelcast-go-client/internal/it/skip"
)
func TestCPMap(t *testing.T) {
skip.IfNot(t, "hz >= 5.4, enterprise")
testCases := []struct {
name string
f func(t *testing.T)
}{
{name: "cpMapCompareAndSet", f: cpMapCompareAndSetTest},
{name: "cpMapCompareAndSet_NilKeyValue", f: cpMapCompareAndSet_NilKeyValueTest},
{name: "cpMapDelete", f: cpMapDeleteTest},
{name: "cpMapDelete_NilKey", f: cpMapDelete_NilKeyTest},
{name: "cpMapGet", f: cpMapGetTest},
{name: "cpMapGet_NilKey", f: cpMapGet_NilKeyTest},
{name: "cpMapPut", f: cpMapPutTest},
{name: "cpMapPut_NilKeyValue", f: cpMapPut_NilKeyValueTest},
{name: "cpMapRemove", f: cpMapRemoveTest},
{name: "cpMapRemove_NilKey", f: cpMapRemove_NilKeyTest},
}
for _, tc := range testCases {
t.Run(tc.name, tc.f)
}
}
func cpMapGetTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
v := check.MustValue(tcx.M.Get(ctx, "key"))
require.Nil(t, v)
check.Must(tcx.M.Set(ctx, "key", "value"))
v = check.MustValue(tcx.M.Get(ctx, "key"))
require.Equal(t, "value", v)
})
}
func cpMapGet_NilKeyTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
// nil key should return an error
_, err := tcx.M.Get(ctx, nil)
require.Error(t, err)
})
}
func cpMapPutTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
old := check.MustValue(tcx.M.Put(ctx, "key", "value"))
require.Nil(t, old)
v := check.MustValue(tcx.M.Get(ctx, "key"))
require.Equal(t, "value", v)
})
}
func cpMapPut_NilKeyValueTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
// nil key should return an error
_, err := tcx.M.Put(ctx, nil, "value")
require.Error(t, err)
// nil value should return an error
_, err = tcx.M.Put(ctx, "foo", nil)
require.Error(t, err)
})
}
func cpMapRemoveTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
v := check.MustValue(tcx.M.Remove(ctx, "key"))
require.Nil(t, v)
check.Must(tcx.M.Set(ctx, "key", "value"))
v = check.MustValue(tcx.M.Remove(ctx, "key"))
require.Equal(t, "value", v)
})
}
func cpMapRemove_NilKeyTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
// nil key should return an error
_, err := tcx.M.Remove(ctx, nil)
require.Error(t, err)
})
}
func cpMapDeleteTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
check.Must(tcx.M.Set(ctx, "key", "value"))
v := check.MustValue(tcx.M.Get(ctx, "key"))
require.Equal(t, "value", v)
check.Must(tcx.M.Delete(ctx, "key"))
v = check.MustValue(tcx.M.Get(ctx, "key"))
require.Equal(t, nil, v)
})
}
func cpMapDelete_NilKeyTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
// nil key should return an error
err := tcx.M.Delete(ctx, nil)
require.Error(t, err)
})
}
func cpMapCompareAndSetTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
ok := check.MustValue(tcx.M.CompareAndSet(ctx, "key", "foo", "bar"))
require.False(t, ok)
check.Must(tcx.M.Set(ctx, "key", "foo"))
ok = check.MustValue(tcx.M.CompareAndSet(ctx, "key", "foo", "bar"))
require.True(t, ok)
})
}
func cpMapCompareAndSet_NilKeyValueTest(t *testing.T) {
tcx := it.CPMapTestContext{T: t}
tcx.Tester(func(tcx *it.CPMapTestContext) {
ctx := context.Background()
// nil key should return an error
_, err := tcx.M.CompareAndSet(ctx, nil, "a", "b")
require.Error(t, err)
// nil expected value should return an error
_, err = tcx.M.CompareAndSet(ctx, "foo", nil, "b")
require.Error(t, err)
// nil new value should return an error
_, err = tcx.M.CompareAndSet(ctx, "foo", "a", nil)
require.Error(t, err)
})
}