1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
package reopen
import (
"io/ioutil"
"os"
"testing"
"time"
)
// TestReopenAppend -- make sure we always append to an existing file
//
// 1. Create a sample file using normal means
// 2. Open a ioreopen.File
// write line 1
// 3. call Reopen
// write line 2
// 4. close file
// 5. read file, make sure it contains line0,line1,line2
//
func TestReopenAppend(t *testing.T) {
// TODO fix
var fname = "/tmp/foo"
// Step 1 -- Create a sample file using normal means
forig, err := os.Create(fname)
if err != nil {
t.Fatalf("Unable to create initial file %s: %s", fname, err)
}
_, err = forig.Write([]byte("line0\n"))
if err != nil {
t.Fatalf("Unable to write initial line %s: %s", fname, err)
}
err = forig.Close()
if err != nil {
t.Fatalf("Unable to close initial file: %s", err)
}
// Test that making a new File appends
f, err := NewFileWriter(fname)
if err != nil {
t.Fatalf("Unable to create %s", fname)
}
_, err = f.Write([]byte("line1\n"))
if err != nil {
t.Errorf("Got write error1: %s", err)
}
// Test that reopen always appends
err = f.Reopen()
if err != nil {
t.Errorf("Got reopen error %s: %s", fname, err)
}
_, err = f.Write([]byte("line2\n"))
if err != nil {
t.Errorf("Got write error2 on %s: %s", fname, err)
}
err = f.Close()
if err != nil {
t.Errorf("Got closing error for %s: %s", fname, err)
}
out, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("Unable read in final file %s: %s", fname, err)
}
outstr := string(out)
if outstr != "line0\nline1\nline2\n" {
t.Errorf("Result was %s", outstr)
}
}
// Test that reopen works when Inode is swapped out
// 1. Create a sample file using normal means
// 2. Open a ioreopen.File
// write line 1
// 3. call Reopen
// write line 2
// 4. close file
// 5. read file, make sure it contains line0,line1,line2
//
func TestChangeInode(t *testing.T) {
// TODO fix
var fname = "/tmp/foo"
// Step 1 -- Create a empty sample file
forig, err := os.Create(fname)
if err != nil {
t.Fatalf("Unable to create initial file %s: %s", fname, err)
}
err = forig.Close()
if err != nil {
t.Fatalf("Unable to close initial file: %s", err)
}
// Test that making a new File appends
f, err := NewFileWriter(fname)
if err != nil {
t.Fatalf("Unable to create %s", fname)
}
_, err = f.Write([]byte("line1\n"))
if err != nil {
t.Errorf("Got write error1: %s", err)
}
// Now move file
err = os.Rename(fname, fname+".orig")
if err != nil {
t.Errorf("Renaming error: %s", err)
}
f.Write([]byte("after1\n"))
// Test that reopen always appends
err = f.Reopen()
if err != nil {
t.Errorf("Got reopen error %s: %s", fname, err)
}
_, err = f.Write([]byte("line2\n"))
if err != nil {
t.Errorf("Got write error2 on %s: %s", fname, err)
}
err = f.Close()
if err != nil {
t.Errorf("Got closing error for %s: %s", fname, err)
}
out, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("Unable read in final file %s: %s", fname, err)
}
outstr := string(out)
if outstr != "line2\n" {
t.Errorf("Result was %s", outstr)
}
}
func TestBufferedWriter(t *testing.T) {
const fname = "/tmp/foo"
fw, err := NewFileWriter(fname)
if err != nil {
t.Fatalf("Unable to create test file %q: %s", fname, err)
}
logger := NewBufferedFileWriterSize(fw, 0, time.Millisecond*100)
// make sure 3-4 flush events happen
time.Sleep(time.Millisecond * 4)
// close should shutdown the flush
logger.Close()
// if flush is still happening we should get one or two here
time.Sleep(time.Millisecond * 200)
}
|