metatest used for both filesystem and redis Cache
(t *testing.T, c Cache)
| 57 | |
| 58 | // metatest used for both filesystem and redis Cache |
| 59 | func cacheAddGetHelper(t *testing.T, c Cache) { |
| 60 | |
| 61 | for i := 0; i < 10; i++ { |
| 62 | key := &Key{ |
| 63 | Query: []byte(fmt.Sprintf("SELECT %d", i)), |
| 64 | } |
| 65 | trw := &testResponseWriter{} |
| 66 | |
| 67 | ct := fmt.Sprintf("text/html; %d", i) |
| 68 | ce := fmt.Sprintf("gzip; %d", i) |
| 69 | value := fmt.Sprintf("value %d", i) |
| 70 | //we want to test what happen we the cache handle a big value |
| 71 | if i == 0 { |
| 72 | // 4MB string |
| 73 | value = strings.Repeat("a", 4*1024*1024) |
| 74 | } |
| 75 | |
| 76 | length := int64(len(value)) |
| 77 | buffer := strings.NewReader(value) |
| 78 | if _, err := c.Put(buffer, ContentMetadata{Encoding: ce, Type: ct, Length: length}, key); err != nil { |
| 79 | t.Fatalf("failed to put it to cache: %s", err) |
| 80 | } |
| 81 | |
| 82 | cachedData, err := c.Get(key) |
| 83 | if err != nil { |
| 84 | t.Fatalf("failed to get data from filesystem cache: %s", err) |
| 85 | } |
| 86 | defer cachedData.Data.Close() |
| 87 | |
| 88 | // Verify trw contains valid headers. |
| 89 | if cachedData.Type != ct { |
| 90 | t.Fatalf("unexpected Content-Type: %s; expecting %s", cachedData.Type, ct) |
| 91 | } |
| 92 | if cachedData.Encoding != ce { |
| 93 | t.Fatalf("unexpected Content-Encoding: %s; expecting %s", cachedData.Encoding, ce) |
| 94 | } |
| 95 | cl := length |
| 96 | if cachedData.Length != cl { |
| 97 | t.Fatalf("unexpected Content-Length: %d; expecting %d", cachedData.Length, cl) |
| 98 | } |
| 99 | buf := new(strings.Builder) |
| 100 | _, err = io.Copy(buf, cachedData.Data) |
| 101 | if err != nil { |
| 102 | t.Fatalf("couldn't read buffer to string %s", err) |
| 103 | } |
| 104 | // Verify trw contains the response. |
| 105 | if buf.String() != value { |
| 106 | logSuffx := "" |
| 107 | if len(value) > maxStringSizeToLog { |
| 108 | logSuffx = "..." |
| 109 | } |
| 110 | t.Fatalf("unexpected response sent to client: %q; expecting %q%s", trw.b, value[:maxStringSizeToLog], logSuffx) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Verify the cache may be re-opened. |
| 115 | for i := 0; i < 10; i++ { |
| 116 | key := &Key{ |
no test coverage detected