Skip to content

Commit 545d284

Browse files
committed
Merge branch 'v3'
2 parents c2e203a + a8f8a8c commit 545d284

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+788
-651
lines changed

.circleci/config.yml

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ workflows:
77
workflow:
88
jobs:
99
- go-test:
10-
name: Go 1.17
11-
docker-image: cimg/go:1.17
10+
name: Go 1.19
11+
docker-image: cimg/go:1.19
12+
run-lint: true
1213
- go-test:
13-
name: Go 1.16
14-
docker-image: cimg/go:1.16
15-
- go-test:
16-
name: Go 1.15
17-
docker-image: cimg/go:1.15
18-
- go-test:
19-
name: Go 1.14
20-
docker-image: cimg/go:1.14
21-
- go-test:
22-
name: Go 1.13
23-
docker-image: cimg/go:1.13
14+
name: Go 1.18
15+
docker-image: cimg/go:1.18
2416
- go-test-windows:
2517
name: Windows
2618

@@ -31,7 +23,7 @@ jobs:
3123
type: string
3224
run-lint:
3325
type: boolean
34-
default: true
26+
default: false
3527

3628
docker:
3729
- image: <<parameters.docker-image>>
@@ -43,7 +35,7 @@ jobs:
4335
- checkout
4436
- run:
4537
name: install go-junit-report
46-
command: go get -u github.com/jstemmer/go-junit-report
38+
command: go install github.com/jstemmer/go-junit-report/[email protected]
4739

4840
- when:
4941
condition: <<parameters.run-lint>>
@@ -74,6 +66,15 @@ jobs:
7466

7567
steps:
7668
- checkout
69+
- run:
70+
name: download Go 1.18.5
71+
command: |
72+
$ErrorActionPreference = "Stop"
73+
$installerUrl = "https://go.dev/dl/go1.18.5.windows-amd64.msi"
74+
(New-Object System.Net.WebClient).DownloadFile($installerUrl, "go1.18.5.windows-amd64.msi")
75+
- run:
76+
name: install Go 1.18.5
77+
command: Start-Process msiexec.exe -Wait -ArgumentList "/I go1.18.5.windows-amd64.msi /quiet"
7778
- run: go version
7879
- run:
7980
name: build and test

.golangci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ linters:
1818
- godox
1919
- gofmt
2020
- goimports
21-
- golint
2221
- gosec
2322
- gosimple
2423
- govet
2524
- ineffassign
26-
- interfacer
2725
- lll
2826
- megacheck
2927
- misspell
3028
- nakedret
3129
- nolintlint
3230
- prealloc
31+
- revive
3332
- staticcheck
34-
- structcheck
3533
- stylecheck
3634
- typecheck
3735
- unconvert

.ldrelease/config.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ version: 2
22

33
jobs:
44
- docker:
5-
image: golang:1.13-buster
5+
image: golang:1.18-buster
66
template:
77
name: go
8-
env:
9-
LD_RELEASE_GO_IMPORT_PATH: github.com/launchdarkly/go-test-helpers
108

119
branches:
1210
- name: main
1311
- name: 2.x
1412

1513
publications:
16-
- url: https://pkg.go.dev/github.com/launchdarkly/go-test-helpers
14+
- url: https://pkg.go.dev/github.com/launchdarkly/go-test-helpers/v2
1715
description: documentation

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
GOLANGCI_LINT_VERSION=v1.27.0
2+
GOLANGCI_LINT_VERSION=v1.48.0
33

44
LINTER=./bin/golangci-lint
55
LINTER_VERSION_FILE=./bin/.golangci-lint-version-$(GOLANGCI_LINT_VERSION)
@@ -17,7 +17,7 @@ test: build
1717

1818
$(LINTER_VERSION_FILE):
1919
rm -f $(LINTER)
20-
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s $(GOLANGCI_LINT_VERSION)
20+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $(GOLANGCI_LINT_VERSION)
2121
touch $(LINTER_VERSION_FILE)
2222

2323
lint: $(LINTER_VERSION_FILE)

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This project centralizes some test support code that is used by LaunchDarkly's G
66

77
While this code may be useful in other projects, it is primarily geared toward LaunchDarkly's own development needs and is not meant to provide a large general-purpose framework. It is meant for unit test code and should not be used as a runtime dependency.
88

9-
This version of the project requires Go 1.13 or higher.
9+
This version of the project requires Go 1.18 or higher.
1010

1111
## Contents
1212

@@ -16,8 +16,6 @@ Subpackage `httphelpers` provides convenience wrappers for using `net/http` and
1616

1717
Subpackage `jsonhelpers` provides functions for manipulating JSON.
1818

19-
Subpackage `ldservices` is specifically for testing LaunchDarkly SDK client components; it provides HTTP handlers that simulate the service endpoints used by the SDK.
20-
2119
Subpackage `matchers` contains a test assertion API with combinators.
2220

2321
Subpackage `testbox` provides the ability to write tests-of-tests within the Go testing framework.

channels.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package helpers
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
// TryReceive waits for a value from the channel and returns (value, true, false) if
12+
// successful; (<empty>, false, false) if the timeout expired first; or
13+
// (<empty>, false, true) if the channel was closed.
14+
func TryReceive[V any](ch <-chan V, timeout time.Duration) (V, bool, bool) {
15+
deadline := time.NewTimer(timeout)
16+
defer deadline.Stop()
17+
select {
18+
case v, ok := <-ch:
19+
if ok {
20+
return v, true, false
21+
}
22+
return v, false, true
23+
case <-deadline.C:
24+
var empty V
25+
return empty, false, false
26+
}
27+
}
28+
29+
// RequireValue returns the next value from the channel, or forces an immediate test failure
30+
// and exit if the timeout expires first.
31+
func RequireValue[V any](t require.TestingT, ch <-chan V, timeout time.Duration, customMessageAndArgs ...any) V {
32+
v, ok, closed := TryReceive(ch, timeout)
33+
if ok {
34+
return v
35+
}
36+
var empty V
37+
if closed {
38+
failWithMessageAndArgs(t, customMessageAndArgs,
39+
"expected a %T value from channel but the channel was closed", empty)
40+
} else {
41+
failWithMessageAndArgs(t, customMessageAndArgs,
42+
"expected a %T value from channel but did not receive one in %s", empty, timeout)
43+
}
44+
t.FailNow()
45+
return empty // never reached
46+
}
47+
48+
// AssertNoMoreValues asserts that no value is available from the channel within the timeout,
49+
// but that the channel was not closed.
50+
func AssertNoMoreValues[V any](
51+
t assert.TestingT,
52+
ch <-chan V,
53+
timeout time.Duration,
54+
customMessageAndArgs ...any,
55+
) bool {
56+
v, ok, closed := TryReceive(ch, timeout)
57+
if ok {
58+
failWithMessageAndArgs(t, customMessageAndArgs,
59+
"expected no more %T values from channel but got one: %+v", v, v)
60+
return false
61+
}
62+
if closed {
63+
failWithMessageAndArgs(t, customMessageAndArgs, "channel was unexpectedly closed")
64+
return false
65+
}
66+
return true
67+
}
68+
69+
// AssertChannelClosed asserts that the channel is closed within the timeout, sending no values.
70+
func AssertChannelClosed[V any](
71+
t assert.TestingT,
72+
ch <-chan V,
73+
timeout time.Duration,
74+
customMessageAndArgs ...any,
75+
) bool {
76+
v, ok, closed := TryReceive(ch, timeout)
77+
if ok {
78+
failWithMessageAndArgs(t, customMessageAndArgs,
79+
"expected no more %T values from channel but got one: %+v", v, v)
80+
return false
81+
}
82+
if !closed {
83+
failWithMessageAndArgs(t, customMessageAndArgs,
84+
"expected channel to be closed within %s but it was not", timeout)
85+
return false
86+
}
87+
return true
88+
}
89+
90+
// AssertChannelNotClosed asserts that the channel is not closed within the timeout, consuming
91+
// any values that may be sent during that time.
92+
func AssertChannelNotClosed[V any](
93+
t assert.TestingT,
94+
ch <-chan V,
95+
timeout time.Duration,
96+
customMessageAndArgs ...any,
97+
) bool {
98+
deadline := time.NewTimer(timeout)
99+
defer deadline.Stop()
100+
for {
101+
select {
102+
case _, ok := <-ch:
103+
if !ok {
104+
failWithMessageAndArgs(t, customMessageAndArgs, "channel was unexpectedly closed")
105+
return false
106+
}
107+
case <-deadline.C:
108+
return true
109+
}
110+
}
111+
}
112+
113+
func failWithMessageAndArgs(t assert.TestingT, customMessageAndArgs []any, defaultMsg string, defaultArgs ...any) {
114+
t.Errorf(defaultMsg, defaultArgs...)
115+
if len(customMessageAndArgs) != 0 {
116+
t.Errorf(fmt.Sprintf("%s", customMessageAndArgs[0]), customMessageAndArgs[1:]...)
117+
}
118+
}

0 commit comments

Comments
 (0)