Просмотр исходного кода

Add observer mock

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
f0063ba089
2 измененных файлов: 40 добавлений и 15 удалений
  1. 22
    0
      events/init_test.go
  2. 18
    15
      ipblocklist/firehol.go

+ 22
- 0
events/init_test.go Просмотреть файл

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 Просмотреть файл

187
 		return fmt.Errorf("cannot open file: %w", err)
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
 	defer filefp.Close()
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
 func (f *Firehol) updateRemoteURL(ctx context.Context, url string,
200
 func (f *Firehol) updateRemoteURL(ctx context.Context, url string,
200
 		return fmt.Errorf("cannot build a request: %w", err)
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
 	if err != nil {
209
 	if err != nil {
205
 		return fmt.Errorf("cannot request a remote URL %s: %w", url, err)
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
 	reader io.Reader,
227
 	reader io.Reader,
219
 	v4tree *bool_tree.TreeV4,
228
 	v4tree *bool_tree.TreeV4,
220
 	v6tree *bool_tree.TreeV6) error {
229
 	v6tree *bool_tree.TreeV6) error {
221
 	scanner := bufio.NewScanner(reader)
230
 	scanner := bufio.NewScanner(reader)
222
 
231
 
223
 	for scanner.Scan() {
232
 	for scanner.Scan() {
224
-		select {
225
-		case <-ctx.Done():
226
-			return ctx.Err()
227
-		default:
228
-		}
229
-
230
 		text := scanner.Text()
233
 		text := scanner.Text()
231
 		text = fireholRegexpComment.ReplaceAllLiteralString(text, "")
234
 		text = fireholRegexpComment.ReplaceAllLiteralString(text, "")
232
 		text = strings.TrimSpace(text)
235
 		text = strings.TrimSpace(text)

Загрузка…
Отмена
Сохранить