Debian Codegraph @

internal/lossy/striped_test.go

raw symbols SCIP
1 // Copyright (c) 2024 Alexey Mayshev and contributors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package lossyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/
16
17 import (
18 "runtimescip-go gomod github.com/golang/go/src go1.24.0 runtime/"
19 "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/"
20 "sync/atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/"
21 "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/"
22
23 "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/"
24
25 "github.com/maypok86/otter/v2/internal/generated/nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/"
26 "github.com/maypok86/otter/v2/internal/xmathscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xmath`/"
27 "github.com/maypok86/otter/v2/internal/xruntimescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/"
28 )
29
30 var validStatusesscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/validStatuses. = map[Statusscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Status#]bool{
31 Successscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Success.: true,
32 Failedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Failed.: true,
33 Fullscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Full.: true,
34 }
35
36 func TestNewStripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/TestNewStriped().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
37 t.Parallelscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Parallel().()
38
39 nm := nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.NewManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/NewManager().[int, int](nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Configscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#{
40 WithSizescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#WithSize.: true,
41 WithExpirationscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#WithExpiration.: true,
42 })
43 s := NewStripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/NewStriped().(64, nm)
44 if got := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().(); got != nil {
45 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("the striped buffer must be nil, but got: %v", got)
46 }
47 res := s.Addscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#Add().(nm.Createscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Manager#Create().(1, 2, 100, 0, 1))
48 if l := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().().lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.; l != 1 {
49 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("the striped buffer length must be 1, but got: %d", l)
50 }
51 if res != Successscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Success. {
52 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("the status must be success, but got: %v", res)
53 }
54 }
55
56 func TestStriped_Addscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/TestStriped_Add().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
57 t.Parallelscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Parallel().()
58
59 parallelism := int(xmathscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xmath`/.RoundUpPowerOf2scip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xmath`/RoundUpPowerOf2().(xruntimescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/.Parallelismscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/Parallelism().()))
60 maxBufferLen := 4 * parallelism
61 goroutines := 100 * parallelism
62
63 nm := nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.NewManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/NewManager().[int, int](nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Configscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#{
64 WithSizescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#WithSize.: true,
65 WithExpirationscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#WithExpiration.: true,
66 })
67 s := NewStripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/NewStriped().(maxBufferLen, nm)
68 n := nm.Createscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Manager#Create().(1, 2, 100, 0, 1)
69
70 var wg syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#
71 wg.Addscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Add().(goroutines)
72 for i := 0; i < goroutines; i++ {
73 go func() {
74 defer wg.Donescip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Done().()
75
76 for j := 0; j < 1000; j++ {
77 res := s.Addscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#Add().(n)
78 if !validStatusesscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/validStatuses.[res] {
79 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("the status must be valid, but got: %v", res)
80 return
81 }
82 runtimescip-go gomod github.com/golang/go/src go1.24.0 runtime/.Goschedscip-go gomod github.com/golang/go/src go1.24.0 runtime/Gosched().()
83 }
84 }()
85 }
86
87 wg.Waitscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Wait().()
88
89 if l := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().().lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.; l > maxBufferLen {
90 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("the striped buffer length must less than %d, but got: %d", maxBufferLen, l)
91 }
92 }
93
94 func TestStriped_DrainToscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/TestStriped_DrainTo().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
95 t.Parallelscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Parallel().()
96
97 nm := nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.NewManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/NewManager().[int, int](nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Configscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#{
98 WithSizescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#WithSize.: true,
99 WithExpirationscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#WithExpiration.: true,
100 })
101 s := NewStripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/NewStriped().(64, nm)
102 n := nm.Createscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Manager#Create().(1, 2, 100, 0, 1)
103
104 var drains int
105 s.DrainToscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#DrainTo().(func(n nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Node#[int, int]) {
106 drains++
107 })
108 if drains != 0 {
109 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("Expected number of drained nodes is 0, but got %d", drains)
110 }
111
112 res := s.Addscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#Add().(n)
113 if l := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().().lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.; l != 1 {
114 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("the striped buffer length must be 1, but got: %d", l)
115 }
116 if res != Successscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Success. {
117 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("the status must be success, but got: %v", res)
118 }
119
120 s.DrainToscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#DrainTo().(func(n nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Node#[int, int]) {
121 drains++
122 })
123 if drains != 1 {
124 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("Expected number of drained nodes is 1, but got %d", drains)
125 }
126 }
127
128 func TestStriped_AddAndDrainscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/TestStriped_AddAndDrain().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
129 t.Parallelscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Parallel().()
130
131 parallelism := int(xmathscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xmath`/.RoundUpPowerOf2scip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xmath`/RoundUpPowerOf2().(xruntimescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/.Parallelismscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/Parallelism().()))
132 maxBufferLen := 4 * parallelism
133 goroutines := 100 * parallelism
134
135 nm := nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.NewManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/NewManager().[int, int](nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Configscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#{
136 WithSizescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#WithSize.: true,
137 WithExpirationscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#WithExpiration.: true,
138 })
139 s := NewStripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/NewStriped().(maxBufferLen, nm)
140 n := nm.Createscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Manager#Create().(1, 2, 100, 0, 1)
141
142 var (
143 wg syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#
144 mu syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#
145 drains atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.Uint64scip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint64#
146 )
147 wg.Addscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Add().(goroutines)
148 for i := 0; i < goroutines; i++ {
149 go func() {
150 defer wg.Donescip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Done().()
151
152 for j := 0; j < 1000; j++ {
153 res := s.Addscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#Add().(n)
154 if !validStatusesscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/validStatuses.[res] {
155 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("the status must be valid, but got: %v", res)
156 return
157 }
158 if res == Fullscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Full. && mu.TryLockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#TryLock().() {
159 s.DrainToscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#DrainTo().(func(n nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Node#[int, int]) {
160 drains.Addscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint64#Add().(1)
161 })
162 mu.Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Unlock().()
163 }
164 runtimescip-go gomod github.com/golang/go/src go1.24.0 runtime/.Goschedscip-go gomod github.com/golang/go/src go1.24.0 runtime/Gosched().()
165 }
166 }()
167 }
168
169 wg.Waitscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Wait().()
170
171 s.DrainToscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#DrainTo().(func(n nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Node#[int, int]) {
172 drains.Addscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint64#Add().(1)
173 })
174
175 if l := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().().lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.; l < 1 || l > maxBufferLen {
176 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("the striped buffer length must be in [1, %d], but got: %d", maxBufferLen, l)
177 }
178 }
179
180 func TestStriped_Lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/TestStriped_Len().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
181 t.Parallelscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Parallel().()
182
183 s := &Stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#[int, int]{}
184 requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/Equal().(t, 0, s.Lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#Len().())
185 nm := nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.NewManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/NewManager().[int, int](nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Configscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Config#{})
186 r := newRingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/newRing().(nm, nm.Createscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Manager#Create().(1, 1, 0, 2, 3))
187 ss := &stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#[int, int]{
188 buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.: make([]atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.Pointerscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#[ringscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ring#[int, int]], 2),
189 lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.: 2,
190 }
191 ss.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[0].Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Store().(r)
192 s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Store().(ss)
193 requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/Equal().(t, 1, s.Lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#Len().())
194 }