| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "fmt" | 9 "fmt" |
| 10 "sync" | 10 "sync" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 states = append(states, ps) | 53 states = append(states, ps) |
| 54 } | 54 } |
| 55 if f.errc != nil { | 55 if f.errc != nil { |
| 56 return states, <-f.errc | 56 return states, <-f.errc |
| 57 } | 57 } |
| 58 return states, nil | 58 return states, nil |
| 59 } | 59 } |
| 60 | 60 |
| 61 func TestChecker(t *testing.T) { | 61 func TestChecker(t *testing.T) { |
| 62 fake := &fakeIsolateService{} | 62 fake := &fakeIsolateService{} |
| 63 » checker := newChecker(fake) | 63 » checker := newChecker(context.Background(), fake) |
| 64 | 64 |
| 65 type itemPair struct { | 65 type itemPair struct { |
| 66 item *Item | 66 item *Item |
| 67 ps *isolatedclient.PushState | 67 ps *isolatedclient.PushState |
| 68 } | 68 } |
| 69 | 69 |
| 70 gotc := make(chan itemPair, 150) | 70 gotc := make(chan itemPair, 150) |
| 71 for i := 0; i < 150; i++ { | 71 for i := 0; i < 150; i++ { |
| 72 item := &Item{ | 72 item := &Item{ |
| 73 Path: fmt.Sprintf("/item/%d", i), | 73 Path: fmt.Sprintf("/item/%d", i), |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 t.Errorf("checker hit count: got %v ; want: %v", got, want) | 106 t.Errorf("checker hit count: got %v ; want: %v", got, want) |
| 107 } | 107 } |
| 108 if got, want := checker.Miss.Count, 150; got != want { | 108 if got, want := checker.Miss.Count, 150; got != want { |
| 109 t.Errorf("checker hit count: got %v ; want: %v", got, want) | 109 t.Errorf("checker hit count: got %v ; want: %v", got, want) |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 func TestCheckerDelay(t *testing.T) { | 113 func TestCheckerDelay(t *testing.T) { |
| 114 batchc := make(chan []*service.HandlersEndpointsV1Digest, 2) | 114 batchc := make(chan []*service.HandlersEndpointsV1Digest, 2) |
| 115 fake := &fakeIsolateService{batchc: batchc} | 115 fake := &fakeIsolateService{batchc: batchc} |
| 116 » checker := newChecker(fake) | 116 » checker := newChecker(context.Background(), fake) |
| 117 | 117 |
| 118 nop := func(item *Item, ps *isolatedclient.PushState) {} | 118 nop := func(item *Item, ps *isolatedclient.PushState) {} |
| 119 checker.AddItem(&Item{Digest: "aaa"}, false, nop) | 119 checker.AddItem(&Item{Digest: "aaa"}, false, nop) |
| 120 checker.AddItem(&Item{Digest: "bbb"}, false, nop) | 120 checker.AddItem(&Item{Digest: "bbb"}, false, nop) |
| 121 <-batchc // Block until a batch is sent. | 121 <-batchc // Block until a batch is sent. |
| 122 checker.AddItem(&Item{Digest: "ccc"}, false, nop) | 122 checker.AddItem(&Item{Digest: "ccc"}, false, nop) |
| 123 | 123 |
| 124 if err := checker.Close(); err != nil { | 124 if err := checker.Close(); err != nil { |
| 125 t.Fatalf("checker.Close: got error %v; want %v", err, nil) | 125 t.Fatalf("checker.Close: got error %v; want %v", err, nil) |
| 126 } | 126 } |
| 127 | 127 |
| 128 // Check that we have 2 batches (of 2 and 1 items respectively). | 128 // Check that we have 2 batches (of 2 and 1 items respectively). |
| 129 if got, want := len(fake.itemBatches), 2; got != want { | 129 if got, want := len(fake.itemBatches), 2; got != want { |
| 130 t.Errorf("checker received %d batches, want %d", got, want) | 130 t.Errorf("checker received %d batches, want %d", got, want) |
| 131 } | 131 } |
| 132 for i, batch := range fake.itemBatches { | 132 for i, batch := range fake.itemBatches { |
| 133 if got, want := len(batch), 2-i; got != want { | 133 if got, want := len(batch), 2-i; got != want { |
| 134 t.Errorf("checker batch[%d] has len %d, want %d", i, got
, want) | 134 t.Errorf("checker batch[%d] has len %d, want %d", i, got
, want) |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 | 138 |
| 139 func TestCheckerErrors(t *testing.T) { | 139 func TestCheckerErrors(t *testing.T) { |
| 140 // Make an error channel which sends errBang on the second receive. | 140 // Make an error channel which sends errBang on the second receive. |
| 141 errc := make(chan error, 2) | 141 errc := make(chan error, 2) |
| 142 » errBang := errors.New("bang!") | 142 » errBang := errors.New("bang") |
| 143 errc <- nil | 143 errc <- nil |
| 144 errc <- errBang | 144 errc <- errBang |
| 145 close(errc) | 145 close(errc) |
| 146 | 146 |
| 147 fake := &fakeIsolateService{errc: errc} | 147 fake := &fakeIsolateService{errc: errc} |
| 148 » checker := newChecker(fake) | 148 » checker := newChecker(context.Background(), fake) |
| 149 | 149 |
| 150 nop := func(item *Item, ps *isolatedclient.PushState) {} | 150 nop := func(item *Item, ps *isolatedclient.PushState) {} |
| 151 for i := 0; i < 150; i++ { | 151 for i := 0; i < 150; i++ { |
| 152 item := &Item{ | 152 item := &Item{ |
| 153 Path: fmt.Sprintf("/item/%d", i), | 153 Path: fmt.Sprintf("/item/%d", i), |
| 154 Digest: isolated.HexDigest(fmt.Sprintf("digest%d", i)), | 154 Digest: isolated.HexDigest(fmt.Sprintf("digest%d", i)), |
| 155 } | 155 } |
| 156 checker.AddItem(item, false, nop) | 156 checker.AddItem(item, false, nop) |
| 157 } | 157 } |
| 158 | 158 |
| 159 if err := checker.Close(); err != errBang { | 159 if err := checker.Close(); err != errBang { |
| 160 t.Fatalf("checker.Close: got error %v; want %v", err, errBang) | 160 t.Fatalf("checker.Close: got error %v; want %v", err, errBang) |
| 161 } | 161 } |
| 162 } | 162 } |
| OLD | NEW |