| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestBlueGitUsageIsCached(t *testing.T) { |
| 12 | dir := t.TempDir() |
| 13 | if err := os.WriteFile(filepath.Join(dir, "a"), bytes.Repeat([]byte("x"), 100), 0o644); err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | srv := &Server{} |
| 17 | n, counting, ok := srv.bluegitUsage(dir) |
| 18 | if !ok || counting || n < 100 { |
| 19 | t.Fatalf("eerste telling %d counting=%v ok=%v", n, counting, ok) |
| 20 | } |
| 21 | if err := os.WriteFile(filepath.Join(dir, "b"), bytes.Repeat([]byte("y"), 8000), 0o644); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | again, counting, ok := srv.bluegitUsage(dir) |
| 25 | if !ok || counting || again != n { |
| 26 | t.Fatalf("cache %d -> %d counting=%v ok=%v", n, again, counting, ok) |
| 27 | } |
| 28 | other := t.TempDir() |
| 29 | if err := os.WriteFile(filepath.Join(other, "c"), bytes.Repeat([]byte("z"), 300), 0o644); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | fresh, counting, ok := srv.bluegitUsage(other) |
| 33 | if !ok || counting || fresh == 0 || fresh == n { |
| 34 | t.Fatalf("andere map %d (was %d) counting=%v ok=%v", fresh, n, counting, ok) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Een trage telling houdt de pagina niet op en loopt daarna door tot het eind. |
| 39 | func TestBlueGitUsageContinuesInBackground(t *testing.T) { |
| 40 | oldWait := usageWait |
| 41 | usageWait = 0 |
| 42 | defer func() { usageWait = oldWait }() |
| 43 | dir := t.TempDir() |
| 44 | for i := 0; i < 50; i++ { |
| 45 | sub := filepath.Join(dir, "d", string(rune('a'+i%26)), string(rune('a'+i/26))) |
| 46 | if err := os.MkdirAll(sub, 0o755); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if err := os.WriteFile(filepath.Join(sub, "f"), bytes.Repeat([]byte("x"), 1000), 0o644); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | } |
| 53 | srv := &Server{} |
| 54 | _, _, _ = srv.bluegitUsage(dir) |
| 55 | deadline := time.Now().Add(5 * time.Second) |
| 56 | for { |
| 57 | n, counting, ok := srv.bluegitUsage(dir) |
| 58 | if ok && !counting { |
| 59 | if n < 50*1000 { |
| 60 | t.Fatalf("onvolledig: %d", n) |
| 61 | } |
| 62 | return |
| 63 | } |
| 64 | if time.Now().After(deadline) { |
| 65 | t.Fatal("telling werd niet afgemaakt") |
| 66 | } |
| 67 | time.Sleep(10 * time.Millisecond) |
| 68 | } |
| 69 | } |