4
4
"context"
5
5
"errors"
6
6
"io/fs"
7
- "os"
8
7
"testing"
9
8
"time"
10
9
@@ -100,7 +99,7 @@ func TestFindMainProcess(t *testing.T) {
100
99
101
100
for _ , test := range tests {
102
101
t .Run (test .name , func (t * testing.T ) {
103
- g := NewGomegaWithT (t )
102
+ g := NewWithT (t )
104
103
105
104
result , err := findMainProcess (test .ctx , test .checkFile , test .readFile , 2 * time .Millisecond )
106
105
@@ -115,30 +114,20 @@ func TestFindMainProcess(t *testing.T) {
115
114
}
116
115
117
116
func TestEnsureNewNginxWorkers (t * testing.T ) {
117
+ previousContents := []byte ("1 2 3" )
118
+ newContents := []byte ("4 5 6" )
119
+
118
120
readFileError := func (string ) ([]byte , error ) {
119
121
return nil , errors .New ("error" )
120
122
}
121
123
122
- tempFileFunc := func (contents []byte ) * os.File {
123
- tempFile , err := os .CreateTemp ("" , "tmpfile-" )
124
- if err != nil {
125
- return nil
126
- }
127
- if _ , err = tempFile .Write (contents ); err != nil {
128
- return nil
129
- }
130
- return tempFile
124
+ readFilePrevious := func (string ) ([]byte , error ) {
125
+ return previousContents , nil
131
126
}
132
127
133
- previousContents := []byte ("1 2 3" )
134
-
135
- childFileSame := tempFileFunc (previousContents )
136
- childFileDifferent := tempFileFunc ([]byte ("4 5 6" ))
137
-
138
- defer childFileSame .Close ()
139
- defer os .Remove (childFileSame .Name ())
140
- defer childFileDifferent .Close ()
141
- defer os .Remove (childFileDifferent .Name ())
128
+ readFileNew := func (string ) ([]byte , error ) {
129
+ return newContents , nil
130
+ }
142
131
143
132
ctx := context .Background ()
144
133
cancellingCtx , cancel := context .WithCancel (ctx )
@@ -147,39 +136,34 @@ func TestEnsureNewNginxWorkers(t *testing.T) {
147
136
tests := []struct {
148
137
ctx context.Context
149
138
readFile readFileFunc
150
- childFile string
151
139
name string
152
140
previousContents []byte
153
141
expectError bool
154
142
}{
155
143
{
156
144
ctx : ctx ,
157
- readFile : os .ReadFile ,
158
- childFile : childFileDifferent .Name (),
145
+ readFile : readFileNew ,
159
146
previousContents : previousContents ,
160
147
expectError : false ,
161
148
name : "normal case" ,
162
149
},
163
150
{
164
151
ctx : ctx ,
165
152
readFile : readFileError ,
166
- childFile : childFileDifferent .Name (),
167
153
previousContents : previousContents ,
168
154
expectError : true ,
169
155
name : "cannot read file" ,
170
156
},
171
157
{
172
158
ctx : ctx ,
173
- readFile : os .ReadFile ,
174
- childFile : childFileSame .Name (),
159
+ readFile : readFilePrevious ,
175
160
previousContents : previousContents ,
176
161
expectError : true ,
177
162
name : "no new workers" ,
178
163
},
179
164
{
180
165
ctx : cancellingCtx ,
181
- readFile : os .ReadFile ,
182
- childFile : childFileSame .Name (),
166
+ readFile : readFilePrevious ,
183
167
previousContents : previousContents ,
184
168
expectError : true ,
185
169
name : "context canceled" ,
@@ -188,11 +172,11 @@ func TestEnsureNewNginxWorkers(t *testing.T) {
188
172
189
173
for _ , test := range tests {
190
174
t .Run (test .name , func (t * testing.T ) {
191
- g := NewGomegaWithT (t )
175
+ g := NewWithT (t )
192
176
193
177
err := ensureNewNginxWorkers (
194
178
test .ctx ,
195
- test . childFile ,
179
+ "/childfile" ,
196
180
test .previousContents ,
197
181
test .readFile ,
198
182
2 * time .Millisecond ,
0 commit comments