forked from hazelcast/hazelcast-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic_ref_it_test.go
129 lines (117 loc) · 3.7 KB
/
atomic_ref_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
/*
* 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/assert"
"github.com/stretchr/testify/require"
"github.com/hazelcast/hazelcast-go-client/internal/check"
"github.com/hazelcast/hazelcast-go-client/internal/it"
)
func TestAtomicRef(t *testing.T) {
testCases := []struct {
name string
f func(t *testing.T)
}{
{name: "atomicRefClear", f: atomicRefClearTest},
{name: "atomicRefCompareAndSet", f: atomicRefCompareAndSetTest},
{name: "atomicRefContains", f: atomicRefContainsTest},
{name: "atomicRefGet", f: atomicRefGetTest},
{name: "atomicRefGetAndSet", f: atomicRefGetAndSetTest},
{name: "atomicRefIsNil", f: atomicRefIsNilTest},
{name: "atomicRefName", f: atomicRefNametest},
}
for _, tc := range testCases {
t.Run(tc.name, tc.f)
}
}
func atomicRefCompareAndSetTest(t *testing.T) {
tcx := it.AtomicRefTestContext{T: t}
tcx.Tester(func(tcx *it.AtomicRefTestContext) {
ctx := context.Background()
ok := check.MustValue(tcx.A.CompareAndSet(ctx, nil, "bar"))
require.True(t, ok)
ok = check.MustValue(tcx.A.CompareAndSet(ctx, "bar", "foo"))
require.True(t, ok)
})
}
func atomicRefGetTest(t *testing.T) {
tcx := it.AtomicRefTestContext{T: t}
tcx.Tester(func(tcx *it.AtomicRefTestContext) {
ctx := context.Background()
value := check.MustValue(tcx.A.Get(ctx))
require.Equal(t, nil, value)
check.Must(tcx.A.Set(ctx, "foo"))
value = check.MustValue(tcx.A.Get(ctx))
require.Equal(t, "foo", value)
})
}
func atomicRefGetAndSetTest(t *testing.T) {
tcx := it.AtomicRefTestContext{T: t}
tcx.Tester(func(tcx *it.AtomicRefTestContext) {
ctx := context.Background()
v1 := check.MustValue(tcx.A.GetAndSet(ctx, "foo"))
require.Equal(t, nil, v1)
v2 := check.MustValue(tcx.A.GetAndSet(ctx, "bar"))
require.Equal(t, "foo", v2)
})
}
func atomicRefContainsTest(t *testing.T) {
tcx := it.AtomicRefTestContext{T: t}
tcx.Tester(func(tcx *it.AtomicRefTestContext) {
ctx := context.Background()
found := check.MustValue(tcx.A.Contains(ctx, "foo"))
require.False(t, found)
check.Must(tcx.A.Set(ctx, "foo"))
found = check.MustValue(tcx.A.Contains(ctx, "foo"))
require.True(t, found)
})
}
func atomicRefIsNilTest(t *testing.T) {
tcx := it.AtomicRefTestContext{T: t}
tcx.Tester(func(tcx *it.AtomicRefTestContext) {
ctx := context.Background()
ok := check.MustValue(tcx.A.IsNil(ctx))
require.True(t, ok)
check.Must(tcx.A.Set(ctx, "foo"))
ok = check.MustValue(tcx.A.IsNil(ctx))
require.False(t, ok)
})
}
func atomicRefClearTest(t *testing.T) {
tcx := it.AtomicRefTestContext{T: t}
tcx.Tester(func(tcx *it.AtomicRefTestContext) {
ctx := context.Background()
check.Must(tcx.A.Set(ctx, "foo"))
v := check.MustValue(tcx.A.Get(ctx))
require.Equal(t, "foo", v)
check.Must(tcx.A.Clear(ctx))
v = check.MustValue(tcx.A.Get(ctx))
require.Equal(t, nil, v)
})
}
func atomicRefNametest(t *testing.T) {
name := it.NewUniqueObjectName("atomicref")
nameWithGroup := name + "@somegroup"
tcx := it.AtomicRefTestContext{
T: t,
Name: nameWithGroup,
}
tcx.Tester(func(tcx *it.AtomicRefTestContext) {
assert.Equal(t, tcx.A.Name(), name)
})
}