github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/mungers/flakesync/cache_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package flakesync 18 19 import ( 20 "reflect" 21 "runtime" 22 "sync" 23 "testing" 24 "time" 25 26 "github.com/google/gofuzz" 27 ) 28 29 func makeRandom(Job, Number) (*Result, error) { 30 r := &Result{} 31 fuzz.New().Fuzz(r) 32 time.Sleep(time.Millisecond / 4) 33 return r, nil 34 } 35 36 func TestBasic(t *testing.T) { 37 c := NewCache(makeRandom) 38 r1, err := c.Get("foo", 5) 39 if err != nil { 40 t.Errorf("unexpected error: %v", err) 41 } 42 r2, _ := c.Get("foo", 5) 43 if !reflect.DeepEqual(r1, r2) { 44 t.Errorf("expected to match: %#v, %#v", r1, r2) 45 } 46 for i := 0; len(c.Flakes()) == 0; i++ { 47 c.Get("foo", Number(i)) 48 } 49 } 50 51 func TestThreading(t *testing.T) { 52 // Test multi-threading even in single threaded environments (like 53 // travis). 54 runtime.GOMAXPROCS(10) 55 c := NewCache(makeRandom) 56 c.maxFlakes = 15 57 wg := sync.WaitGroup{} 58 const threads = 30 59 wg.Add(threads) 60 for i := 0; i < threads; i++ { 61 go func(s int) { 62 defer wg.Done() 63 for n := 0; n < 30; n++ { 64 // n*s means many collide a few times, but some do not 65 c.Get("foo", Number(n*s)) 66 } 67 if len(c.Flakes()) > 15 { 68 t.Errorf("Max flakes doesn't seem to work, got %v", len(c.Flakes())) 69 } 70 }(i) 71 } 72 wg.Wait() 73 if len(c.Flakes()) > 15 { 74 t.Errorf("Max flakes doesn't seem to work, got %v", len(c.Flakes())) 75 } 76 }