|
7 | 7 | "bytes" |
8 | 8 | "os" |
9 | 9 | "path/filepath" |
| 10 | + "slices" |
10 | 11 | "sync" |
11 | 12 | "testing" |
12 | 13 | "time" |
@@ -83,6 +84,49 @@ func TestTailchonkFS_CommitTime(t *testing.T) { |
83 | 84 | } |
84 | 85 | } |
85 | 86 |
|
| 87 | +// If we were interrupted while writing a temporary file, AllAUMs() |
| 88 | +// should ignore it when scanning the AUM directory. |
| 89 | +func TestTailchonkFS_IgnoreTempFile(t *testing.T) { |
| 90 | + base := t.TempDir() |
| 91 | + chonk := must.Get(ChonkDir(base)) |
| 92 | + parentHash := randHash(t, 1) |
| 93 | + aum := AUM{MessageKind: AUMNoOp, PrevAUMHash: parentHash[:]} |
| 94 | + must.Do(chonk.CommitVerifiedAUMs([]AUM{aum})) |
| 95 | + |
| 96 | + writeAUMFile := func(filename, contents string) { |
| 97 | + t.Helper() |
| 98 | + if err := os.MkdirAll(filepath.Join(base, filename[0:2]), os.ModePerm); err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + if err := os.WriteFile(filepath.Join(base, filename[0:2], filename), []byte(contents), 0600); err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Check that calling AllAUMs() returns the single committed AUM |
| 107 | + got, err := chonk.AllAUMs() |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("AllAUMs() failed: %v", err) |
| 110 | + } |
| 111 | + want := []AUMHash{aum.Hash()} |
| 112 | + if !slices.Equal(got, want) { |
| 113 | + t.Fatalf("AllAUMs() is wrong: got %v, want %v", got, want) |
| 114 | + } |
| 115 | + |
| 116 | + // Write some temporary files which are named like partially-committed AUMs, |
| 117 | + // then check that AllAUMs() only returns the single committed AUM. |
| 118 | + writeAUMFile("AUM1234.tmp", "incomplete AUM\n") |
| 119 | + writeAUMFile("AUM1234.tmp_123", "second incomplete AUM\n") |
| 120 | + |
| 121 | + got, err = chonk.AllAUMs() |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("AllAUMs() failed: %v", err) |
| 124 | + } |
| 125 | + if !slices.Equal(got, want) { |
| 126 | + t.Fatalf("AllAUMs() is wrong: got %v, want %v", got, want) |
| 127 | + } |
| 128 | +} |
| 129 | + |
86 | 130 | func TestMarkActiveChain(t *testing.T) { |
87 | 131 | type aumTemplate struct { |
88 | 132 | AUM AUM |
|
0 commit comments