Skip to content

Commit 48e6959

Browse files
authored
Mock Windows signal handler (#276)
1 parent 1865257 commit 48e6959

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

internal/scanner/scanner.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"math/rand"
1111
"net/http"
1212
"net/url"
13-
"os"
14-
"os/signal"
1513
"regexp"
1614
"strings"
1715
"sync"
@@ -321,32 +319,11 @@ func (s *Scanner) Run(ctx context.Context) error {
321319
// separately.
322320
var requestsCounter uint64
323321

324-
userSignal := make(chan os.Signal, 1)
325-
signal.Notify(userSignal, syscall.SIGUSR1)
326-
defer func() {
327-
signal.Stop(userSignal)
328-
close(userSignal)
329-
}()
330-
331-
go func() {
332-
for {
333-
select {
334-
case _, ok := <-userSignal:
335-
if !ok {
336-
return
337-
}
338-
339-
s.logger.
340-
WithFields(logrus.Fields{
341-
"sent": atomic.LoadUint64(&requestsCounter),
342-
"total": s.db.NumberOfTests,
343-
}).Info("Testing status")
344-
345-
case <-ctx.Done():
346-
return
347-
}
348-
}
349-
}()
322+
closeListener, err := s.testStatusSignalHandler(ctx, &requestsCounter)
323+
if err != nil {
324+
return err
325+
}
326+
defer closeListener()
350327

351328
for e := 0; e < gn; e++ {
352329
go func() {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package scanner
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/signal"
7+
"sync/atomic"
8+
"syscall"
9+
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
func (s *Scanner) testStatusSignalHandler(ctx context.Context, requestsCounter *uint64) (func(), error) {
14+
userSignal := make(chan os.Signal, 1)
15+
signal.Notify(userSignal, syscall.SIGUSR1)
16+
17+
go func() {
18+
for {
19+
select {
20+
case _, ok := <-userSignal:
21+
if !ok {
22+
return
23+
}
24+
25+
s.logger.
26+
WithFields(logrus.Fields{
27+
"sent": atomic.LoadUint64(requestsCounter),
28+
"total": s.db.NumberOfTests,
29+
}).Info("Testing status")
30+
31+
case <-ctx.Done():
32+
return
33+
}
34+
}
35+
}()
36+
37+
return func() {
38+
signal.Stop(userSignal)
39+
close(userSignal)
40+
}, nil
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scanner
2+
3+
import (
4+
"context"
5+
)
6+
7+
func (s *Scanner) testStatusSignalHandler(_ context.Context, _ *uint64) (func(), error) {
8+
return func() {}, nil
9+
}

0 commit comments

Comments
 (0)