Add observer mock

This commit is contained in:
9seconds
2021-03-16 10:51:43 +03:00
parent b08d945d7c
commit f0063ba089
2 changed files with 40 additions and 15 deletions
+22
View File
@@ -0,0 +1,22 @@
package events_test
import (
"github.com/9seconds/mtg/v2/mtglib"
"github.com/stretchr/testify/mock"
)
type ObserverMock struct {
mock.Mock
}
func (o *ObserverMock) EventStart(evt mtglib.EventStart) {
o.Called(evt)
}
func (o *ObserverMock) EventFinish(evt mtglib.EventStart) {
o.Called(evt)
}
func (o *ObserverMock) Shutdown() {
o.Called()
}
+18 -15
View File
@@ -187,9 +187,14 @@ func (f *Firehol) updateLocalFile(ctx context.Context, filename string,
return fmt.Errorf("cannot open file: %w", err) return fmt.Errorf("cannot open file: %w", err)
} }
go func(ctx context.Context, closer io.Closer) {
<-ctx.Done()
closer.Close()
}(ctx, filefp)
defer filefp.Close() defer filefp.Close()
return f.updateTrees(ctx, mutex, filefp, v4tree, v6tree) return f.updateTrees(mutex, filefp, v4tree, v6tree)
} }
func (f *Firehol) updateRemoteURL(ctx context.Context, url string, func (f *Firehol) updateRemoteURL(ctx context.Context, url string,
@@ -200,33 +205,31 @@ func (f *Firehol) updateRemoteURL(ctx context.Context, url string,
return fmt.Errorf("cannot build a request: %w", err) return fmt.Errorf("cannot build a request: %w", err)
} }
resp, err := f.httpClient.Do(req) resp, err := f.httpClient.Do(req) // nolint: bodyclose
if err != nil { if err != nil {
return fmt.Errorf("cannot request a remote URL %s: %w", url, err) return fmt.Errorf("cannot request a remote URL %s: %w", url, err)
} }
defer func() { go func(ctx context.Context, closer io.Closer) {
io.Copy(ioutil.Discard, resp.Body) // nolint: errcheck <-ctx.Done()
resp.Body.Close() closer.Close()
}() }(ctx, resp.Body)
return f.updateTrees(ctx, mutex, resp.Body, v4tree, v6tree) defer func(rc io.ReadCloser) {
io.Copy(ioutil.Discard, rc) // nolint: errcheck
rc.Close()
}(resp.Body)
return f.updateTrees(mutex, resp.Body, v4tree, v6tree)
} }
func (f *Firehol) updateTrees(ctx context.Context, func (f *Firehol) updateTrees(mutex sync.Locker,
mutex sync.Locker,
reader io.Reader, reader io.Reader,
v4tree *bool_tree.TreeV4, v4tree *bool_tree.TreeV4,
v6tree *bool_tree.TreeV6) error { v6tree *bool_tree.TreeV6) error {
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)
for scanner.Scan() { for scanner.Scan() {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
text := scanner.Text() text := scanner.Text()
text = fireholRegexpComment.ReplaceAllLiteralString(text, "") text = fireholRegexpComment.ReplaceAllLiteralString(text, "")
text = strings.TrimSpace(text) text = strings.TrimSpace(text)