diff options
author | Steven Angles <[email protected]> | 2021-08-16 17:04:47 -0400 |
---|---|---|
committer | GitHub <[email protected]> | 2021-08-16 15:04:47 -0600 |
commit | a10910f3981908424493c043d26dfcb4e5f8dc25 (patch) | |
tree | 71dfde574dd47d9ddc1d965b4470b4e2864124de /admin_test.go | |
parent | ab32440b2121c5a8cf36e1eea1b7347f0386c2dc (diff) | |
download | caddy-a10910f3981908424493c043d26dfcb4e5f8dc25.tar.gz caddy-a10910f3981908424493c043d26dfcb4e5f8dc25.zip |
admin: Sync server variables (fix #4260) (#4274)
* Synchronize server assignment/references to avoid data race
* only hold lock during var reassignment
Diffstat (limited to 'admin_test.go')
-rw-r--r-- | admin_test.go | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/admin_test.go b/admin_test.go index cfb4ab7bf..608a32c67 100644 --- a/admin_test.go +++ b/admin_test.go @@ -17,9 +17,28 @@ package caddy import ( "encoding/json" "reflect" + "sync" "testing" ) +var testCfg = []byte(`{ + "apps": { + "http": { + "servers": { + "myserver": { + "listen": ["tcp/localhost:8080-8084"], + "read_timeout": "30s" + }, + "yourserver": { + "listen": ["127.0.0.1:5000"], + "read_header_timeout": "15s" + } + } + } + } + } + `) + func TestUnsyncedConfigAccess(t *testing.T) { // each test is performed in sequence, so // each change builds on the previous ones; @@ -108,25 +127,24 @@ func TestUnsyncedConfigAccess(t *testing.T) { } } +// TestLoadConcurrent exercises Load under concurrent conditions +// and is most useful under test with `-race` enabled. +func TestLoadConcurrent(t *testing.T) { + var wg sync.WaitGroup + + for i := 0; i < 100; i++ { + wg.Add(1) + go func() { + _ = Load(testCfg, true) + wg.Done() + }() + } + + wg.Wait() +} + func BenchmarkLoad(b *testing.B) { for i := 0; i < b.N; i++ { - cfg := []byte(`{ - "apps": { - "http": { - "servers": { - "myserver": { - "listen": ["tcp/localhost:8080-8084"], - "read_timeout": "30s" - }, - "yourserver": { - "listen": ["127.0.0.1:5000"], - "read_header_timeout": "15s" - } - } - } - } - } - `) - Load(cfg, true) + Load(testCfg, true) } } |