tendermint/autofile/autofile_test.go

72 lines
1.7 KiB
Go
Raw Normal View History

2016-10-26 16:23:19 -07:00
package autofile
import (
"os"
"sync/atomic"
"syscall"
"testing"
"time"
2017-10-03 09:18:21 -07:00
cmn "github.com/tendermint/tmlibs/common"
2016-10-26 16:23:19 -07:00
)
func TestSIGHUP(t *testing.T) {
// First, create an AutoFile writing to a tempfile dir
2017-10-03 09:18:21 -07:00
file, name := cmn.Tempfile("sighup_test")
if err := file.Close(); err != nil {
2016-10-26 16:23:19 -07:00
t.Fatalf("Error creating tempfile: %v", err)
}
// Here is the actual AutoFile
2017-10-03 21:13:58 -07:00
af, err := OpenAutoFile(name)
2016-10-26 16:23:19 -07:00
if err != nil {
t.Fatalf("Error creating autofile: %v", err)
}
// Write to the file.
_, err = af.Write([]byte("Line 1\n"))
if err != nil {
t.Fatalf("Error writing to autofile: %v", err)
}
_, err = af.Write([]byte("Line 2\n"))
if err != nil {
t.Fatalf("Error writing to autofile: %v", err)
}
// Move the file over
2017-10-03 21:13:58 -07:00
err = os.Rename(name, name+"_old")
if err != nil {
2016-10-26 16:23:19 -07:00
t.Fatalf("Error moving autofile: %v", err)
}
// Send SIGHUP to self.
oldSighupCounter := atomic.LoadInt32(&sighupCounter)
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
// Wait a bit... signals are not handled synchronously.
for atomic.LoadInt32(&sighupCounter) == oldSighupCounter {
time.Sleep(time.Millisecond * 10)
}
// Write more to the file.
_, err = af.Write([]byte("Line 3\n"))
if err != nil {
t.Fatalf("Error writing to autofile: %v", err)
}
_, err = af.Write([]byte("Line 4\n"))
if err != nil {
t.Fatalf("Error writing to autofile: %v", err)
}
2017-10-03 09:18:21 -07:00
if err := af.Close(); err != nil {
2016-10-26 16:23:19 -07:00
t.Fatalf("Error closing autofile")
}
// Both files should exist
2017-10-03 09:18:21 -07:00
if body := cmn.MustReadFile(name + "_old"); string(body) != "Line 1\nLine 2\n" {
2016-10-26 16:23:19 -07:00
t.Errorf("Unexpected body %s", body)
}
2017-10-03 09:18:21 -07:00
if body := cmn.MustReadFile(name); string(body) != "Line 3\nLine 4\n" {
2016-10-26 16:23:19 -07:00
t.Errorf("Unexpected body %s", body)
}
}