Commit fd8dc815 authored by Luke Champine's avatar Luke Champine

fix journal.checkpoint on windows

parent 909c4485
......@@ -114,7 +114,7 @@ func TestContract(t *testing.T) {
// TestContracts tests the Contracts method.
func TestContracts(t *testing.T) {
var stub newStub
dir := build.TempDir("contractor", "TestNew")
dir := build.TempDir("contractor", "TestContracts")
c, err := New(stub, stub, stub, stub, dir)
if err != nil {
t.Fatalf("expected nil, got %v", err)
......
......@@ -85,15 +85,19 @@ func (j *journal) checkpoint(obj interface{}) error {
}
// atomically replace the old file with the new one
if err := tmp.Close(); err != nil {
return err
}
if err := j.f.Close(); err != nil {
return err
}
if err := os.Rename(j.filename+"_tmp", j.filename); err != nil {
if err := os.Rename(tmp.Name(), j.filename); err != nil {
return err
}
j.f = tmp
return nil
// reopen the journal
j.f, err = os.OpenFile(j.filename, os.O_RDWR|os.O_APPEND, 0)
return err
}
// Close closes the underlying file.
......@@ -118,7 +122,10 @@ func openJournal(filename string, obj interface{}) (*journal, error) {
f: f,
filename: filename,
}
if err := j.checkpoint(obj); err != nil {
if err := json.NewEncoder(f).Encode(obj); err != nil {
return nil, err
}
if err := f.Sync(); err != nil {
return nil, err
}
return j, nil
......
package contractor
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/NebulousLabs/Sia/build"
)
func tempFile(t interface {
Fatal(...interface{})
}, name string) (*os.File, func()) {
f, err := ioutil.TempFile("", name)
f, err := os.Create(filepath.Join(build.TempDir("contractor", name)))
if err != nil {
t.Fatal(err)
}
......@@ -22,18 +24,13 @@ func tempFile(t interface {
func tempJournal(t interface {
Fatal(...interface{})
}, obj interface{}, name string) (*journal, func()) {
f, err := ioutil.TempFile("", name)
if err != nil {
t.Fatal(err)
}
f.Close()
j, err := openJournal(f.Name(), obj)
j, err := openJournal(filepath.Join(build.TempDir("contractor", name)), obj)
if err != nil {
t.Fatal(err)
}
return j, func() {
j.Close()
os.RemoveAll(f.Name())
os.RemoveAll(j.filename)
}
}
......@@ -72,6 +69,36 @@ func TestJournal(t *testing.T) {
}
}
func TestJournalCheckpoint(t *testing.T) {
type bar struct {
Z int `json:"z"`
}
type foo struct {
X int `json:"x"`
Y []bar `json:"y"`
}
j, cleanup := tempJournal(t, foo{Y: []bar{}}, "TestJournalCheckpoint")
defer cleanup()
if err := j.checkpoint(bar{3}); err != nil {
t.Fatal(err)
}
if err := j.Close(); err != nil {
t.Fatal(err)
}
var b bar
j2, err := openJournal(j.filename, &b)
if err != nil {
t.Fatal(err)
}
j2.Close()
if b.Z != 3 {
t.Fatal("checkpoint failed:", b.Z)
}
}
func TestJournalMalformed(t *testing.T) {
f, cleanup := tempFile(t, "TestJournalMalformed")
defer cleanup()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment