+// TestPutBlockTouchFails
+// When PutBlock is asked to PUT an existing block, but cannot
+// modify the timestamp, it should write a second block.
+//
+func TestPutBlockTouchFails(t *testing.T) {
+ defer teardown()
+
+ // Prepare two test Keep volumes.
+ KeepVM = MakeTestVolumeManager(2)
+ defer func() { KeepVM.Quit() }()
+ vols := KeepVM.Volumes()
+
+ // Store a block and then make the underlying volume bad,
+ // so a subsequent attempt to update the file timestamp
+ // will fail.
+ vols[0].Put(TEST_HASH, BAD_BLOCK)
+ old_mtime, err := vols[0].Mtime(TEST_HASH)
+ if err != nil {
+ t.Fatalf("vols[0].Mtime(%s): %s\n", TEST_HASH, err)
+ }
+
+ // vols[0].Touch will fail on the next call, so the volume
+ // manager will store a copy on vols[1] instead.
+ vols[0].(*MockVolume).Touchable = false
+ if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
+ t.Fatalf("PutBlock: %v", err)
+ }
+ vols[0].(*MockVolume).Touchable = true
+
+ // Now the mtime on the block on vols[0] should be unchanged, and
+ // there should be a copy of the block on vols[1].
+ new_mtime, err := vols[0].Mtime(TEST_HASH)
+ if err != nil {
+ t.Fatalf("vols[0].Mtime(%s): %s\n", TEST_HASH, err)
+ }
+ if !new_mtime.Equal(old_mtime) {
+ t.Errorf("mtime was changed on vols[0]:\nold_mtime = %v\nnew_mtime = %v\n",
+ old_mtime, new_mtime)
+ }
+ result, err := vols[1].Get(TEST_HASH)
+ if err != nil {
+ t.Fatalf("vols[1]: %v", err)
+ }
+ if bytes.Compare(result, TEST_BLOCK) != 0 {
+ t.Errorf("new block does not match test block\nnew block = %v\n", result)
+ }
+}
+