MCPcopy Create free account
hub / github.com/encodeous/nylon / TestConcurrencySafety

Function TestConcurrencySafety

polyamide/device/device_test.go:245–324  ·  view source on GitHub ↗

TestConcurrencySafety does other things concurrently with tunnel use. It is intended to be used with the race detector to catch data races.

(t *testing.T)

Source from the content-addressed store, hash-verified

243// TestConcurrencySafety does other things concurrently with tunnel use.
244// It is intended to be used with the race detector to catch data races.
245func TestConcurrencySafety(t *testing.T) {
246 pair := genTestPair(t, true)
247 done := make(chan struct{})
248
249 const warmupIters = 10
250 var warmup sync.WaitGroup
251 warmup.Add(warmupIters)
252 go func() {
253 // Send data continuously back and forth until we're done.
254 // Note that we may continue to attempt to send data
255 // even after done is closed.
256 i := warmupIters
257 for ping := Ping; ; ping = !ping {
258 pair.Send(t, ping, done)
259 select {
260 case <-done:
261 return
262 default:
263 }
264 if i > 0 {
265 warmup.Done()
266 i--
267 }
268 }
269 }()
270 warmup.Wait()
271
272 applyCfg := func(cfg string) {
273 err := pair[0].dev.IpcSet(cfg)
274 if err != nil {
275 t.Fatal(err)
276 }
277 }
278
279 // Change persistent_keepalive_interval concurrently with tunnel use.
280 t.Run("persistentKeepaliveInterval", func(t *testing.T) {
281 var pub NoisePublicKey
282 for key := range pair[0].dev.peers.keyMap {
283 pub = key
284 break
285 }
286 cfg := uapiCfg(
287 "public_key", hex.EncodeToString(pub[:]),
288 "persistent_keepalive_interval", "1",
289 )
290 for i := 0; i < 1000; i++ {
291 applyCfg(cfg)
292 }
293 })
294
295 // Change private keys concurrently with tunnel use.
296 t.Run("privateKey", func(t *testing.T) {
297 bad := uapiCfg("private_key", "7777777777777777777777777777777777777777777777777777777777777777")
298 good := uapiCfg("private_key", hex.EncodeToString(pair[0].dev.staticIdentity.privateKey[:]))
299 // Set iters to a large number like 1000 to flush out data races quickly.
300 // Don't leave it large. That can cause logical races
301 // in which the handshake is interleaved with key changes
302 // such that the private key appears to be unchanging but

Callers

nothing calls this directly

Calls 7

genTestPairFunction · 0.85
uapiCfgFunction · 0.85
IpcSetMethod · 0.80
BindUpdateMethod · 0.80
SendMethod · 0.65
WaitMethod · 0.45

Tested by

no test coverage detected