This works but we have issue on golog.SetLevel and get golog.Level on httptest.New when tests are running in parallel and the loggers are used. TODO: Fix it on golog repository or here, we'll see. func TestCacheHandlerParallel(t *testing.T) { t.Parallel() TestCache(t) }
(t *testing.T)
| 154 | // } |
| 155 | |
| 156 | func TestCacheValidator(t *testing.T) { |
| 157 | app := iris.New() |
| 158 | var n uint32 |
| 159 | |
| 160 | h := func(ctx *context.Context) { |
| 161 | atomic.AddUint32(&n, 1) |
| 162 | ctx.Write([]byte(expectedBodyStr)) |
| 163 | } |
| 164 | |
| 165 | validCache := cache.Handler(cacheDuration) |
| 166 | app.Get("/", validCache, h) |
| 167 | |
| 168 | managedCache := cache.Cache(cache.MaxAge(cacheDuration)) |
| 169 | managedCache.AddRule(rule.Validator([]rule.PreValidator{ |
| 170 | func(ctx *context.Context) bool { |
| 171 | // should always invalid for cache, don't bother to go to try to get or set cache |
| 172 | return ctx.Request().URL.Path != "/invalid" |
| 173 | }, |
| 174 | }, nil)) |
| 175 | |
| 176 | managedCache2 := cache.Cache(cache.MaxAge(cacheDuration)) |
| 177 | managedCache2.AddRule(rule.Validator(nil, |
| 178 | []rule.PostValidator{ |
| 179 | func(ctx *context.Context) bool { |
| 180 | // it's passed the Claim and now Valid checks if the response contains a header of "DONT" |
| 181 | return ctx.ResponseWriter().Header().Get("DONT") == "" |
| 182 | }, |
| 183 | }, |
| 184 | )) |
| 185 | |
| 186 | app.Get("/valid", validCache, h) |
| 187 | |
| 188 | app.Get("/invalid", managedCache.ServeHTTP, h) |
| 189 | app.Get("/invalid2", managedCache2.ServeHTTP, func(ctx *context.Context) { |
| 190 | atomic.AddUint32(&n, 1) |
| 191 | ctx.Header("DONT", "DO not cache that response even if it was claimed") |
| 192 | ctx.Write([]byte(expectedBodyStr)) |
| 193 | }) |
| 194 | |
| 195 | e := httptest.New(t, app) |
| 196 | |
| 197 | // execute from cache the next time |
| 198 | e.GET("/valid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) |
| 199 | time.Sleep(cacheDuration / 5) // lets wait for a while, cache should be saved and ready |
| 200 | e.GET("/valid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) |
| 201 | counter := atomic.LoadUint32(&n) |
| 202 | if counter > 1 { |
| 203 | // n should be 1 because it doesn't changed after the first call |
| 204 | t.Fatalf("%s: %v", t.Name(), &testError{1, counter}) |
| 205 | } |
| 206 | // don't execute from cache, execute the original, counter should ++ here |
| 207 | e.GET("/invalid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter = 2 |
| 208 | e.GET("/invalid2").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter = 3 |
| 209 | |
| 210 | counter = atomic.LoadUint32(&n) |
| 211 | if counter != 3 { |
| 212 | // n should be 1 because it doesn't changed after the first call |
| 213 | t.Fatalf("%s: %v", t.Name(), &testError{3, counter}) |
nothing calls this directly
no test coverage detected
searching dependent graphs…