|
1 | 1 | package bufferedstorage_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
5 | 6 | "os"
|
6 | 7 | "path"
|
@@ -129,6 +130,41 @@ func TestSubdir(t *testing.T) {
|
129 | 130 | assertFileExists(t, ctx, buffer, "test/test.txt")
|
130 | 131 | }
|
131 | 132 |
|
| 133 | +func TestSameContent(t *testing.T) { |
| 134 | + const testContent = "Hello world!" |
| 135 | + backingDir := t.TempDir() |
| 136 | + backingStorage := tofutestutils.Must2(filesystemstorage.New(backingDir)) |
| 137 | + |
| 138 | + ctx := tofutestutils.Context(t) |
| 139 | + |
| 140 | + tofutestutils.Must(backingStorage.WriteFile(ctx, "test.txt", []byte(testContent))) |
| 141 | + |
| 142 | + localDir := t.TempDir() |
| 143 | + buffer := tofutestutils.Must2(bufferedstorage.New(logger.NewTestLogger(t), localDir, backingStorage, 25)) |
| 144 | + if _, err := os.Stat(path.Join(localDir, "test.txt")); err == nil { |
| 145 | + t.Fatalf("Test file was present before fetched.") |
| 146 | + } |
| 147 | + readData, err := buffer.ReadFile(ctx, "test.txt") |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("Could not read test file: %v", err) |
| 150 | + } |
| 151 | + if !bytes.Equal(readData, []byte(testContent)) { |
| 152 | + t.Fatalf("The test file has incorrect contents: %s", readData) |
| 153 | + } |
| 154 | + |
| 155 | + if _, err = os.Stat(path.Join(localDir, "test.txt")); err != nil { |
| 156 | + t.Fatalf("Test file was not present before after fetching: %v", err) |
| 157 | + } |
| 158 | + tofutestutils.Must(buffer.WriteFile(ctx, "test.txt", []byte(testContent))) |
| 159 | + if buffer.UncommittedFiles() != 0 { |
| 160 | + t.Fatalf("Uncommitted files despite same content.") |
| 161 | + } |
| 162 | + tofutestutils.Must(buffer.WriteFile(ctx, "test.txt", []byte(testContent+"!"))) |
| 163 | + if buffer.UncommittedFiles() != 1 { |
| 164 | + t.Fatalf("No or multiple uncommitted files despite different content!") |
| 165 | + } |
| 166 | +} |
| 167 | + |
132 | 168 | func assertFileDoesNotExist(t *testing.T, ctx context.Context, storage indexstorage.API, file indexstorage.Path) {
|
133 | 169 | t.Helper()
|
134 | 170 | _, err := storage.ReadFile(ctx, file)
|
|
0 commit comments