checkpoint_paths_test.mx raw

   1  // The checkpoint paths the round-trip tests do not reach: Open on something
   2  // that is not a file, Set's create failure and Set's rename failure.
   3  package checkpoint
   4  
   5  import (
   6  	"os"
   7  	"testing"
   8  )
   9  
  10  func TestOpenReadError(t *testing.T) {
  11  	dir, ok := cpTmp(t)
  12  	if !ok {
  13  		return
  14  	}
  15  	// A directory at the checkpoint path is not a missing file: the read error
  16  	// must be reported, not treated as "no checkpoint".
  17  	if err := os.MkdirAll(dir|"/checkpoint.dat", 0755); err != nil {
  18  		t.Fatal(err)
  19  	}
  20  	if _, err := Open(dir | "/checkpoint.dat"); err == nil {
  21  		t.Fatal("Open must report a path it cannot read")
  22  	}
  23  	os.RemoveAll(dir)
  24  }
  25  
  26  func TestSetCreateFails(t *testing.T) {
  27  	dir, ok := cpTmp(t)
  28  	if !ok {
  29  		return
  30  	}
  31  	// The checkpoint's directory does not exist, so the temporary file cannot
  32  	// be created.
  33  	c, oerr := Open(dir | "/missing/checkpoint.dat")
  34  	if oerr != nil {
  35  		t.Fatal(oerr)
  36  	}
  37  	if serr := c.Set(7); serr == nil {
  38  		t.Fatal("Set must report a temporary file it cannot create")
  39  	}
  40  	if c.Get() != 0 {
  41  		t.Fatal("a failed Set must not advance the serial")
  42  	}
  43  	os.RemoveAll(dir)
  44  }
  45  
  46  func TestSetRenameFails(t *testing.T) {
  47  	dir, ok := cpTmp(t)
  48  	if !ok {
  49  		return
  50  	}
  51  	// The destination is a directory, so the rename cannot replace it. Open
  52  	// itself rejects a directory, so the checkpoint is built directly.
  53  	if err := os.MkdirAll(dir|"/checkpoint.dat", 0755); err != nil {
  54  		t.Fatal(err)
  55  	}
  56  	c := &Checkpoint{path: dir | "/checkpoint.dat"}
  57  	if serr := c.Set(7); serr == nil {
  58  		t.Fatal("Set must report a rename that cannot replace the destination")
  59  	}
  60  	if c.Get() != 0 {
  61  		t.Fatal("a failed Set must not advance the serial")
  62  	}
  63  	// The failed Set must not leave its temporary file behind.
  64  	if _, statErr := os.Stat(dir | "/checkpoint.dat.tmp"); statErr == nil {
  65  		t.Fatal("a failed Set left checkpoint.dat.tmp behind")
  66  	}
  67  	os.RemoveAll(dir)
  68  }
  69