This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathfilewriter_test.go
161 lines (147 loc) · 4.45 KB
/
filewriter_test.go
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
153
154
155
156
157
158
159
160
161
// This file is a modified redistribution of reopen (github.com/client9/reopen),
// which is governed by the following license notice:
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Nick Galbreath
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package logger
import (
"os"
"testing"
"github.com/featurebasedb/featurebase/v3/testhook"
)
// 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) {
forig, err := testhook.TempFile(t, "logger-reopen")
if err != nil {
t.Fatalf("unable to create initial file: %v", err)
}
fname := forig.Name()
_, 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 := os.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. That is to say,
// if the previous file has been renamed, we want to get a new file
// that isn't the old one, not keep writing to the old file.
//
// 1. Create a sample file using normal means
// 2. Write to it.
// 3. Rename it.
// 4. Call reopen.
// 5. Write line 2.
// 6. Read file, expecting to see only line 2.
func TestChangeInode(t *testing.T) {
// Step 1 -- Create a empty sample file
forig, err := testhook.TempFile(t, "changeInode")
if err != nil {
t.Fatalf("Unable to create initial file: %s", err)
}
fname := forig.Name()
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)
}
// remove the scratch file
defer os.Remove(fname + ".orig")
_, err = f.Write([]byte("after1\n"))
if err != nil {
t.Errorf("Write error: %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 := os.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)
}
}