github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/ghcache/partitioner_test.go (about) 1 /* 2 Copyright 2020 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 ghcache 18 19 import ( 20 "net/http" 21 "sync" 22 "testing" 23 "time" 24 ) 25 26 type fakeRoundTripperCreator struct { 27 t *testing.T 28 lock *sync.Mutex 29 roundTrippers map[string]*fakeRoundTripper 30 } 31 32 func (frtc *fakeRoundTripperCreator) createRoundTripper(partitionKey string, expiresAt *time.Time) http.RoundTripper { 33 frtc.lock.Lock() 34 defer frtc.lock.Unlock() 35 _, alreadyExists := frtc.roundTrippers[partitionKey] 36 if alreadyExists { 37 frtc.t.Fatalf("creation of already existing partition %q was requested", partitionKey) 38 } 39 frtc.roundTrippers[partitionKey] = &fakeRoundTripper{lock: &sync.Mutex{}} 40 return frtc.roundTrippers[partitionKey] 41 } 42 43 type fakeRoundTripper struct { 44 lock *sync.Mutex 45 used bool 46 } 47 48 func (frt *fakeRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { 49 frt.lock.Lock() 50 frt.used = true 51 frt.lock.Unlock() 52 return &http.Response{}, nil 53 } 54 55 func TestPartitioningRoundTripper(t *testing.T) { 56 creator := &fakeRoundTripperCreator{ 57 t: t, 58 lock: &sync.Mutex{}, 59 roundTrippers: map[string]*fakeRoundTripper{}, 60 } 61 partitioningRoundTripper := newPartitioningRoundTripper(creator.createRoundTripper) 62 63 requests := []*http.Request{ 64 {Header: http.Header(map[string][]string{"Authorization": {"a"}})}, 65 {Header: http.Header(map[string][]string{"Authorization": {"a"}})}, 66 {Header: http.Header(map[string][]string{"Authorization": {"b"}})}, 67 {Header: http.Header(map[string][]string{"Authorization": {"b"}})}, 68 {Header: http.Header(map[string][]string{"Authorization": {"c"}})}, 69 {Header: http.Header(map[string][]string{"Authorization": {"c"}})}, 70 } 71 72 // Do these in parallel to verify thread safety 73 wg := &sync.WaitGroup{} 74 for _, request := range requests { 75 wg.Add(1) 76 request := request 77 go func() { 78 defer wg.Done() 79 _, err := partitioningRoundTripper.RoundTrip(request) 80 if err != nil { 81 t.Errorf("RoundTrip: %v", err) 82 } 83 }() 84 } 85 wg.Wait() 86 87 if n := len(creator.roundTrippers); n != 3 { 88 t.Errorf("expected three roundtrippers, got %d (%v)", n, creator.roundTrippers) 89 } 90 for name, roundTripper := range creator.roundTrippers { 91 if !roundTripper.used { 92 t.Errorf("roundtripper %q wasnt used", name) 93 } 94 } 95 }