| 1 |
// Copyright 2019 Huan Du. All rights reserved. |
| 2 |
// Licensed under the MIT license that can be found in the LICENSE file. |
| 3 |
|
| 4 |
package clone |
| 5 |
|
| 6 |
import ( |
| 7 |
"crypto/elliptic" |
| 8 |
"reflect" |
| 9 |
"sync" |
| 10 |
"sync/atomic" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
"unsafe" |
| 14 |
|
| 15 |
"github.com/huandu/go-assert" |
| 16 |
) |
| 17 |
|
| 18 |
type NoPointer struct { |
| 19 |
Foo int |
| 20 |
Bar string |
| 21 |
} |
| 22 |
|
| 23 |
type WithPointer struct { |
| 24 |
foo map[string]string |
| 25 |
bar []int |
| 26 |
} |
| 27 |
|
| 28 |
func TestMarkAsScalar(t *testing.T) { |
| 29 |
a := assert.New(t) |
| 30 |
oldCnt := 0 |
| 31 |
newCnt := 0 |
| 32 |
a.Use(&oldCnt, &newCnt) |
| 33 |
|
| 34 |
// Count cache. |
| 35 |
defaultAllocator.cachedStructTypes.Range(func(key, value interface{}) bool { |
| 36 |
oldCnt++ |
| 37 |
return true |
| 38 |
}) |
| 39 |
|
| 40 |
// Add 2 valid types. |
| 41 |
MarkAsScalar(reflect.TypeOf(new(NoPointer))) |
| 42 |
MarkAsScalar(reflect.TypeOf(new(WithPointer))) |
| 43 |
MarkAsScalar(reflect.TypeOf(new(int))) // Should be ignored. |
| 44 |
|
| 45 |
// Count cache against. |
| 46 |
defaultAllocator.cachedStructTypes.Range(func(key, value interface{}) bool { |
| 47 |
newCnt++ |
| 48 |
return true |
| 49 |
}) |
| 50 |
|
| 51 |
a.Assert(oldCnt+2 == newCnt) |
| 52 |
|
| 53 |
// As WithPointer is marked as scalar, Clone returns a shadow copy. |
| 54 |
value := &WithPointer{ |
| 55 |
foo: map[string]string{ |
| 56 |
"key": "value", |
| 57 |
}, |
| 58 |
bar: []int{1, 2, 3}, |
| 59 |
} |
| 60 |
cloned := Clone(value).(*WithPointer) |
| 61 |
a.Use(&value, &cloned) |
| 62 |
|
| 63 |
// cloned is a shadow copy. |
| 64 |
a.Equal(value, cloned) |
| 65 |
value.foo["key"] = "modified" |
| 66 |
value.bar[1] = 2000 |
| 67 |
a.Equal(value, cloned) |
| 68 |
} |
| 69 |
|
| 70 |
type MapKeys struct { |
| 71 |
mb map[bool]interface{} |
| 72 |
mi map[int]interface{} |
| 73 |
mi8 map[int8]interface{} |
| 74 |
mi16 map[int16]interface{} |
| 75 |
mi32 map[int32]interface{} |
| 76 |
mi64 map[int64]interface{} |
| 77 |
mui map[uint]interface{} |
| 78 |
mu8 map[uint8]interface{} |
| 79 |
mu16 map[uint16]interface{} |
| 80 |
mu32 map[uint32]interface{} |
| 81 |
mu64 map[uint64]interface{} |
| 82 |
muintptr map[uintptr]interface{} |
| 83 |
ms map[string]interface{} |
| 84 |
mf32 map[float32]interface{} |
| 85 |
mf64 map[float64]interface{} |
| 86 |
mc64 map[complex64]interface{} |
| 87 |
mc128 map[complex128]interface{} |
| 88 |
miface map[interface{}]interface{} |
| 89 |
mis map[Simple]interface{} |
| 90 |
// misp map[*Simple]interface{} |
| 91 |
munsafe map[unsafe.Pointer]interface{} |
| 92 |
} |
| 93 |
|
| 94 |
func TestCopyScalarValue(t *testing.T) { |
| 95 |
a := assert.New(t) |
| 96 |
st := &MapKeys{ |
| 97 |
mb: map[bool]interface{}{true: 2}, |
| 98 |
mi: map[int]interface{}{-1: 2}, |
| 99 |
mi8: map[int8]interface{}{-8: 2}, |
| 100 |
mi16: map[int16]interface{}{-16: 2}, |
| 101 |
mi32: map[int32]interface{}{-32: 2}, |
| 102 |
mi64: map[int64]interface{}{-64: 2}, |
| 103 |
mui: map[uint]interface{}{1: 2}, |
| 104 |
mu8: map[uint8]interface{}{8: 2}, |
| 105 |
mu16: map[uint16]interface{}{16: 2}, |
| 106 |
mu32: map[uint32]interface{}{32: 2}, |
| 107 |
mu64: map[uint64]interface{}{64: 2}, |
| 108 |
muintptr: map[uintptr]interface{}{0xDEADC0DE: 2}, |
| 109 |
ms: map[string]interface{}{"str": 2}, |
| 110 |
mf32: map[float32]interface{}{3.2: 2}, |
| 111 |
mf64: map[float64]interface{}{6.4: 2}, |
| 112 |
mc64: map[complex64]interface{}{complex(6, 4): 2}, |
| 113 |
mc128: map[complex128]interface{}{complex(1.2, 8): 2}, |
| 114 |
miface: map[interface{}]interface{}{"iface": 2}, |
| 115 |
mis: map[Simple]interface{}{{Foo: 123}: 2}, |
| 116 |
munsafe: map[unsafe.Pointer]interface{}{unsafe.Pointer(t): 2}, |
| 117 |
} |
| 118 |
cloned := Clone(st).(*MapKeys) |
| 119 |
|
| 120 |
a.Equal(st, cloned) |
| 121 |
} |
| 122 |
|
| 123 |
type noCopyValues struct { |
| 124 |
syncMutex sync.Mutex |
| 125 |
syncRWMutex sync.RWMutex |
| 126 |
syncWaitGroup sync.WaitGroup |
| 127 |
syncCond *sync.Cond |
| 128 |
syncPool sync.Pool |
| 129 |
syncMap sync.Map |
| 130 |
syncOnce sync.Once |
| 131 |
atomicValue atomic.Value |
| 132 |
} |
| 133 |
|
| 134 |
func TestCloneNoCopyValues(t *testing.T) { |
| 135 |
a := assert.New(t) |
| 136 |
v := &noCopyValues{ |
| 137 |
syncCond: sync.NewCond(func() *sync.Mutex { |
| 138 |
return &sync.Mutex{} |
| 139 |
}()), |
| 140 |
syncPool: sync.Pool{ |
| 141 |
New: func() interface{} { |
| 142 |
return "pool" |
| 143 |
}, |
| 144 |
}, |
| 145 |
} |
| 146 |
|
| 147 |
v.syncMutex.Lock() |
| 148 |
defer v.syncMutex.Unlock() |
| 149 |
v.syncRWMutex.RLock() |
| 150 |
defer v.syncRWMutex.RUnlock() |
| 151 |
v.syncWaitGroup.Add(1) |
| 152 |
defer v.syncWaitGroup.Done() |
| 153 |
v.syncCond.L.Lock() |
| 154 |
defer v.syncCond.L.Unlock() |
| 155 |
poolValue := v.syncPool.Get() |
| 156 |
v.syncPool.Put(poolValue) |
| 157 |
v.syncMap.Store("foo", "bar") |
| 158 |
v.syncOnce.Do(func() {}) |
| 159 |
v.atomicValue.Store("value") |
| 160 |
|
| 161 |
cloned := Clone(v).(*noCopyValues) |
| 162 |
done := make(chan bool, 1) |
| 163 |
ticker := time.NewTicker(100 * time.Millisecond) |
| 164 |
defer ticker.Stop() |
| 165 |
|
| 166 |
a.Run("race", func(t *testing.T) { |
| 167 |
a := assert.New(t) |
| 168 |
|
| 169 |
cloned.syncMutex.Lock() |
| 170 |
_ = 0 |
| 171 |
cloned.syncMutex.Unlock() |
| 172 |
|
| 173 |
cloned.syncRWMutex.RLock() |
| 174 |
_ = 0 |
| 175 |
cloned.syncRWMutex.RUnlock() |
| 176 |
|
| 177 |
cloned.syncWaitGroup.Add(1) |
| 178 |
cloned.syncWaitGroup.Done() |
| 179 |
cloned.syncWaitGroup.Wait() |
| 180 |
|
| 181 |
cloned.syncCond.L.Lock() |
| 182 |
_ = 0 |
| 183 |
cloned.syncCond.L.Unlock() |
| 184 |
|
| 185 |
poolValue := cloned.syncPool.Get() |
| 186 |
a.Equal(poolValue, "pool") |
| 187 |
|
| 188 |
mapValue, ok := cloned.syncMap.Load("foo") |
| 189 |
a.Equal(mapValue, "bar") |
| 190 |
a.Assert(ok) |
| 191 |
|
| 192 |
onceValueShouldBeTrue := true |
| 193 |
cloned.syncOnce.Do(func() { |
| 194 |
onceValueShouldBeTrue = false |
| 195 |
}) |
| 196 |
a.Assert(onceValueShouldBeTrue) |
| 197 |
|
| 198 |
value := cloned.atomicValue.Load() |
| 199 |
a.Equal(value, "value") |
| 200 |
|
| 201 |
done <- true |
| 202 |
}) |
| 203 |
|
| 204 |
select { |
| 205 |
case <-done: |
| 206 |
case <-ticker.C: |
| 207 |
a.Fatalf("unexpected lock is detected.") |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestCloneCurveAsScalar(t *testing.T) { |
| 212 |
a := assert.New(t) |
| 213 |
curves := []elliptic.Curve{elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521()} |
| 214 |
cloned := Clone(curves).([]elliptic.Curve) |
| 215 |
|
| 216 |
for i, curve := range curves { |
| 217 |
c := cloned[i] |
| 218 |
a.Assert(curve == c) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
type testOpaquePointer struct { |
| 223 |
foo int |
| 224 |
} |
| 225 |
|
| 226 |
func TestMarkAsOpaquePointer(t *testing.T) { |
| 227 |
a := assert.New(t) |
| 228 |
|
| 229 |
// Mark *testOpaquePointer as opaque pointer. |
| 230 |
MarkAsOpaquePointer(reflect.TypeOf(&testOpaquePointer{})) |
| 231 |
|
| 232 |
// No-op if set a struct type as opaque. |
| 233 |
MarkAsOpaquePointer(reflect.TypeOf(testOpaquePointer{})) |
| 234 |
|
| 235 |
opaque := &testOpaquePointer{ |
| 236 |
foo: 1234, |
| 237 |
} |
| 238 |
cloned := Clone(&opaque).(**testOpaquePointer) |
| 239 |
|
| 240 |
a.Assert(&opaque != cloned) |
| 241 |
a.Assert(opaque == *cloned) |
| 242 |
} |