Skip to content

Commit 103ab81

Browse files
committed
Refactor tests to use mock requests, not servers
1 parent 4d35c6c commit 103ab81

File tree

11 files changed

+158
-128
lines changed

11 files changed

+158
-128
lines changed

.circleci/config.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ workflows:
1010
- test
1111
- test-windows
1212
- test-mac
13+
- test-mac-12-go-1-18
1314
- release:
1415
# Only run this job on git tag pushes
1516
filters:
@@ -60,6 +61,26 @@ jobs:
6061
command: |
6162
mkdir -p /tmp/logs
6263
go test -v ./... --timeout 5m | tee /tmp/logs/test.log
64+
65+
66+
# Specifically test Go 1.18 on mac to catch syscall issues
67+
test-mac-12-go-1-18:
68+
macos:
69+
xcode: 12.5.1
70+
steps:
71+
- checkout
72+
- run:
73+
name: install go
74+
command: |
75+
brew install go@1.18
76+
# Make Go available in the PATH upon first being installed
77+
echo 'export PATH="/usr/local/opt/go@1.18/bin:$PATH"' >> /Users/distiller/.bash_profile
78+
- run:
79+
name: run tests
80+
command: |
81+
mkdir -p /tmp/logs
82+
go test -v ./... --timeout 5m | tee /tmp/logs/test.log
83+
6384
release:
6485
docker:
6586
- image: cimg/go:1.17

cli.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ func RunCLI() error {
4343
}
4444
log.SetLevel(level)
4545

46-
if parseErr := parseBlockListInput(blockList); parseErr != nil {
46+
p := NewProcrastiproxy()
47+
48+
if parseErr := parseBlockListInput(blockList, p.GetList()); parseErr != nil {
4749
return parseErr
4850
}
4951

5052
if parseErr := parseStartAndEndTimes(*blockStartTime, *blockEndTime); parseErr != nil {
5153
return parseErr
5254
}
5355

54-
p := NewProcrastiproxy()
5556
// Configure proxy time-based block settings
5657
p.ConfigureProxyTimeSettings(*blockStartTime, *blockEndTime)
5758

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.14
55
require (
66
github.com/hashicorp/go-multierror v1.1.1
77
github.com/sirupsen/logrus v1.8.1
8+
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
1212
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
1313
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
1414
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
15+
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0=
16+
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (p *Procrastiproxy) timeAwareHandler(w http.ResponseWriter, r *http.Request
7777

7878
func (p *Procrastiproxy) blockListAwareHandler(w http.ResponseWriter, r *http.Request) {
7979
host := sanitizeHost(r.URL.Host)
80-
if hostIsBlocked(host) {
80+
if hostIsOnBlockList(host, p.GetList()) {
8181
log.Debugf("Blocking request to host: %s. User explicitly blocked and present time is within configured proxy block window", host)
8282
blockRequest(w)
8383
return
@@ -94,7 +94,7 @@ func (p *Procrastiproxy) adminHandler(w http.ResponseWriter, r *http.Request) {
9494
log.Println(err)
9595
}
9696
var respMsg string
97-
list := GetList()
97+
list := p.GetList()
9898
if adminCmd.Command == "block" {
9999
list.Add(adminCmd.Host)
100100
respMsg = fmt.Sprintf("Successfully added: %s to the block list\n", adminCmd.Host)

list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sync"
55
)
66

7-
var list = newList()
7+
var list = NewList()
88

99
type List struct {
1010
m sync.Mutex
@@ -16,7 +16,7 @@ func GetList() *List {
1616
return list
1717
}
1818

19-
func newList() *List {
19+
func NewList() *List {
2020
return &List{
2121
members: make(map[string]bool),
2222
}

procrastiproxy.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ var procrastiproxy *Procrastiproxy
1414
var DefaultNow = time.Now
1515

1616
type Procrastiproxy struct {
17-
Now func() time.Time
17+
Now func() time.Time
18+
List *List
1819
ProxyTimeSettings
1920
}
2021

@@ -23,11 +24,16 @@ func NewProcrastiproxy() *Procrastiproxy {
2324
return procrastiproxy
2425
}
2526
procrastiproxy = &Procrastiproxy{
26-
Now: DefaultNow,
27+
Now: DefaultNow,
28+
List: NewList(),
2729
}
2830
return procrastiproxy
2931
}
3032

33+
func (p *Procrastiproxy) GetList() *List {
34+
return p.List
35+
}
36+
3137
// custom errors
3238

3339
type EmptyBlockListError struct{}

server.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ func sanitizeHost(host string) string {
1313
return strings.ToLower(strings.TrimSpace(strings.Replace(host, "\n", "", -1)))
1414
}
1515

16-
func hostIsBlocked(host string) bool {
16+
func hostIsOnBlockList(host string, list *List) bool {
1717
host = sanitizeHost(host)
18-
blockList := GetList()
19-
return blockList.Contains(host)
18+
return list.Contains(host)
2019
}
2120

2221
func RunServer(args []string) {

0 commit comments

Comments
 (0)