/
atomic.go
164 lines (141 loc) · 3.49 KB
/
atomic.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
/*
Copyright (c) Microsoft Corporation.
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 fake
import (
"bytes"
"encoding/json"
"log"
"math"
"sync"
)
// AtomicPtr is intended for use in mocks to easily expose variables for use in testing. It makes setting and retrieving
// the values race free by wrapping the pointer itself in a mutex. There is no Get() method, but instead a Clone() method
// deep copies the object being stored by serializing/de-serializing it from JSON. This pattern shouldn't be followed
// anywhere else but is an easy way to eliminate races in our tests.
// nolint: structcheck
type AtomicPtr[T any] struct {
mu sync.Mutex
value *T
}
func (a *AtomicPtr[T]) Set(v *T) {
a.mu.Lock()
defer a.mu.Unlock()
a.value = v
}
func (a *AtomicPtr[T]) IsNil() bool {
a.mu.Lock()
defer a.mu.Unlock()
return a.value == nil
}
func (a *AtomicPtr[T]) Clone() *T {
a.mu.Lock()
defer a.mu.Unlock()
return clone(a.value)
}
func clone[T any](v *T) *T {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(v); err != nil {
log.Fatalf("encoding %T, %s", v, err)
}
dec := json.NewDecoder(&buf)
var cp T
if err := dec.Decode(&cp); err != nil {
log.Fatalf("encoding %T, %s", v, err)
}
return &cp
}
func (a *AtomicPtr[T]) Reset() {
a.mu.Lock()
defer a.mu.Unlock()
a.value = nil
}
type AtomicError struct {
mu sync.Mutex
err error
calls int
maxCalls int
}
func (e *AtomicError) Reset() {
e.mu.Lock()
defer e.mu.Unlock()
e.err = nil
e.calls = 0
e.maxCalls = 0
}
func (e *AtomicError) IsNil() bool {
e.mu.Lock()
defer e.mu.Unlock()
return e.err == nil
}
// Get is equivalent to the error being called, so we increase
// number of calls in this function
func (e *AtomicError) Get() error {
e.mu.Lock()
defer e.mu.Unlock()
if e.calls >= e.maxCalls {
return nil
}
e.calls++
return e.err
}
func (e *AtomicError) Set(err error, opts ...AtomicErrorOption) {
e.mu.Lock()
defer e.mu.Unlock()
e.err = err
for _, opt := range opts {
opt(e)
}
if e.maxCalls == 0 {
e.maxCalls = 1
}
}
type AtomicErrorOption func(atomicError *AtomicError)
func MaxCalls(maxCalls int) AtomicErrorOption {
// Setting to 0 is equivalent to allowing infinite errors to API
if maxCalls <= 0 {
maxCalls = math.MaxInt
}
return func(e *AtomicError) {
e.maxCalls = maxCalls
}
}
// AtomicPtrStack exposes a slice of a pointer type in a race-free manner. The interface is just enough to replace the
// set.Set usage in our previous tests.
// nolint: structcheck
type AtomicPtrStack[T any] struct {
mu sync.Mutex
values []*T
}
func (a *AtomicPtrStack[T]) Reset() {
a.mu.Lock()
defer a.mu.Unlock()
a.values = nil
}
func (a *AtomicPtrStack[T]) Add(input *T) {
a.mu.Lock()
defer a.mu.Unlock()
a.values = append(a.values, clone(input))
}
func (a *AtomicPtrStack[T]) Len() int {
a.mu.Lock()
defer a.mu.Unlock()
return len(a.values)
}
func (a *AtomicPtrStack[T]) Pop() *T {
a.mu.Lock()
defer a.mu.Unlock()
last := a.values[len(a.values)-1]
a.values = a.values[0 : len(a.values)-1]
return last
}