menu_cache_test.go (1961B)
1 // Copyright 2021 The Hugo Authors. 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 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package navigation 15 16 import ( 17 "sync" 18 "sync/atomic" 19 "testing" 20 21 qt "github.com/frankban/quicktest" 22 ) 23 24 func createSortTestMenu(num int) Menu { 25 menu := make(Menu, num) 26 for i := 0; i < num; i++ { 27 m := &MenuEntry{} 28 menu[i] = m 29 } 30 return menu 31 } 32 33 func TestMenuCache(t *testing.T) { 34 t.Parallel() 35 c := qt.New(t) 36 c1 := newMenuCache() 37 38 changeFirst := func(m Menu) { 39 m[0].title = "changed" 40 } 41 42 var o1 uint64 43 var o2 uint64 44 45 var wg sync.WaitGroup 46 47 var l1 sync.Mutex 48 var l2 sync.Mutex 49 50 var testMenuSets []Menu 51 52 for i := 0; i < 50; i++ { 53 testMenuSets = append(testMenuSets, createSortTestMenu(i+1)) 54 } 55 56 for j := 0; j < 100; j++ { 57 wg.Add(1) 58 go func() { 59 defer wg.Done() 60 for k, menu := range testMenuSets { 61 l1.Lock() 62 m, ca := c1.get("k1", nil, menu) 63 c.Assert(ca, qt.Equals, !atomic.CompareAndSwapUint64(&o1, uint64(k), uint64(k+1))) 64 l1.Unlock() 65 m2, c2 := c1.get("k1", nil, m) 66 c.Assert(c2, qt.Equals, true) 67 c.Assert(menuEqual(m, m2), qt.Equals, true) 68 c.Assert(menuEqual(m, menu), qt.Equals, true) 69 c.Assert(m, qt.Not(qt.IsNil)) 70 71 l2.Lock() 72 m3, c3 := c1.get("k2", changeFirst, menu) 73 c.Assert(c3, qt.Equals, !atomic.CompareAndSwapUint64(&o2, uint64(k), uint64(k+1))) 74 l2.Unlock() 75 c.Assert(m3, qt.Not(qt.IsNil)) 76 c.Assert("changed", qt.Equals, m3[0].title) 77 } 78 }() 79 } 80 wg.Wait() 81 }