Selaa lähdekoodia

Add observer mock

tags/v2.0.0-rc1
9seconds 5 vuotta sitten
vanhempi
commit
f0063ba089
2 muutettua tiedostoa jossa 40 lisäystä ja 15 poistoa
  1. 22
    0
      events/init_test.go
  2. 18
    15
      ipblocklist/firehol.go

+ 22
- 0
events/init_test.go Näytä tiedosto

@@ -0,0 +1,22 @@
1
+package events_test
2
+
3
+import (
4
+	"github.com/9seconds/mtg/v2/mtglib"
5
+	"github.com/stretchr/testify/mock"
6
+)
7
+
8
+type ObserverMock struct {
9
+	mock.Mock
10
+}
11
+
12
+func (o *ObserverMock) EventStart(evt mtglib.EventStart) {
13
+	o.Called(evt)
14
+}
15
+
16
+func (o *ObserverMock) EventFinish(evt mtglib.EventStart) {
17
+	o.Called(evt)
18
+}
19
+
20
+func (o *ObserverMock) Shutdown() {
21
+	o.Called()
22
+}

+ 18
- 15
ipblocklist/firehol.go Näytä tiedosto

@@ -187,9 +187,14 @@ func (f *Firehol) updateLocalFile(ctx context.Context, filename string,
187 187
 		return fmt.Errorf("cannot open file: %w", err)
188 188
 	}
189 189
 
190
+	go func(ctx context.Context, closer io.Closer) {
191
+		<-ctx.Done()
192
+		closer.Close()
193
+	}(ctx, filefp)
194
+
190 195
 	defer filefp.Close()
191 196
 
192
-	return f.updateTrees(ctx, mutex, filefp, v4tree, v6tree)
197
+	return f.updateTrees(mutex, filefp, v4tree, v6tree)
193 198
 }
194 199
 
195 200
 func (f *Firehol) updateRemoteURL(ctx context.Context, url string,
@@ -200,33 +205,31 @@ func (f *Firehol) updateRemoteURL(ctx context.Context, url string,
200 205
 		return fmt.Errorf("cannot build a request: %w", err)
201 206
 	}
202 207
 
203
-	resp, err := f.httpClient.Do(req)
208
+	resp, err := f.httpClient.Do(req) // nolint: bodyclose
204 209
 	if err != nil {
205 210
 		return fmt.Errorf("cannot request a remote URL %s: %w", url, err)
206 211
 	}
207 212
 
208
-	defer func() {
209
-		io.Copy(ioutil.Discard, resp.Body) // nolint: errcheck
210
-		resp.Body.Close()
211
-	}()
213
+	go func(ctx context.Context, closer io.Closer) {
214
+		<-ctx.Done()
215
+		closer.Close()
216
+	}(ctx, resp.Body)
212 217
 
213
-	return f.updateTrees(ctx, mutex, resp.Body, v4tree, v6tree)
218
+	defer func(rc io.ReadCloser) {
219
+		io.Copy(ioutil.Discard, rc) // nolint: errcheck
220
+		rc.Close()
221
+	}(resp.Body)
222
+
223
+	return f.updateTrees(mutex, resp.Body, v4tree, v6tree)
214 224
 }
215 225
 
216
-func (f *Firehol) updateTrees(ctx context.Context,
217
-	mutex sync.Locker,
226
+func (f *Firehol) updateTrees(mutex sync.Locker,
218 227
 	reader io.Reader,
219 228
 	v4tree *bool_tree.TreeV4,
220 229
 	v6tree *bool_tree.TreeV6) error {
221 230
 	scanner := bufio.NewScanner(reader)
222 231
 
223 232
 	for scanner.Scan() {
224
-		select {
225
-		case <-ctx.Done():
226
-			return ctx.Err()
227
-		default:
228
-		}
229
-
230 233
 		text := scanner.Text()
231 234
 		text = fireholRegexpComment.ReplaceAllLiteralString(text, "")
232 235
 		text = strings.TrimSpace(text)

Loading…
Peruuta
Tallenna