Skip to content

Commit e2b098b

Browse files
committed
Add rivertype.JobStates with full list of job states
Requested in #296, add a new `JobStates()` function that contains a complete list of all `JobState*` constant values, which may be useful in places like testing. Also add a test that does a basic parsing of the corresponding Go file, look for all `JobState*` values, and makes sure that `JobStates()` contains every possible value to prevent future regressions. Fixes #296.
1 parent 3c4f8ee commit e2b098b

File tree

8 files changed

+118
-17
lines changed

8 files changed

+118
-17
lines changed

riverdriver/riverdatabasesql/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/lib/pq v1.10.9
1111
github.com/riverqueue/river/riverdriver v0.2.0
1212
github.com/riverqueue/river/rivertype v0.2.0
13-
github.com/stretchr/testify v1.8.1
13+
github.com/stretchr/testify v1.9.0
1414
)
1515

1616
require (

riverdriver/riverdatabasesql/go.sum

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
32
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
43
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
54
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -17,18 +16,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
1716
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
1817
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
1918
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
20-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
21-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
22-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
23-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
24-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
25-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
26-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
19+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
20+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2721
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2822
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2923
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
3024
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
3125
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
32-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3326
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3427
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

riverdriver/riverpgxv5/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/jackc/puddle/v2 v2.2.1
1212
github.com/riverqueue/river/riverdriver v0.2.0
1313
github.com/riverqueue/river/rivertype v0.2.0
14-
github.com/stretchr/testify v1.8.1
14+
github.com/stretchr/testify v1.9.0
1515
)
1616

1717
require (

riverdriver/riverpgxv5/go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
1919
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
2020
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
2121
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
22-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
23-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
2422
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2523
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
26-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
27-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
28-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
29-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
24+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
25+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3026
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
3127
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
3228
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=

rivertype/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/riverqueue/river/rivertype
22

33
go 1.21.4
4+
5+
require github.com/stretchr/testify v1.9.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

rivertype/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

rivertype/river_type.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ const (
112112
JobStateScheduled JobState = "scheduled"
113113
)
114114

115+
// JobStates returns all possible job states.
116+
func JobStates() []JobState {
117+
return []JobState{
118+
JobStateAvailable,
119+
JobStateCancelled,
120+
JobStateCompleted,
121+
JobStateDiscarded,
122+
JobStateRetryable,
123+
JobStateRunning,
124+
JobStateScheduled,
125+
}
126+
}
127+
115128
// AttemptError is an error from a single job attempt that failed due to an
116129
// error or a panic.
117130
type AttemptError struct {

rivertype/river_type_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package rivertype_test
2+
3+
import (
4+
"go/ast"
5+
"go/parser"
6+
"go/token"
7+
"os"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/riverqueue/river/rivertype"
13+
)
14+
15+
func TestJobStates(t *testing.T) {
16+
t.Parallel()
17+
18+
jobStates := rivertype.JobStates()
19+
20+
// One easy check that doesn't require the source file reading below.
21+
require.Contains(t, jobStates, rivertype.JobStateAvailable)
22+
23+
// Get all job state names from the corresponding source file and make sure
24+
// they're included in JobStates. Helps check that we didn't add a new value
25+
// but forgot to add it to the full list of constant values.
26+
for _, nameAndValue := range allValuesForStringConstantType(t, "river_type.go", "JobState") {
27+
t.Logf("Checking for job state: %s / %s", nameAndValue.Name, nameAndValue.Value)
28+
require.Contains(t, jobStates, rivertype.JobState(nameAndValue.Value))
29+
}
30+
}
31+
32+
// stringConstantNameAndValue is a name and value for a string constant like
33+
// `JobStateAvailable` + `available`.
34+
type stringConstantNameAndValue struct{ Name, Value string }
35+
36+
// allValuesForStringConstantType reads a Go source file and looks for all
37+
// values for the named string constant.
38+
func allValuesForStringConstantType(t *testing.T, srcFile, typeName string) []stringConstantNameAndValue {
39+
t.Helper()
40+
41+
fset := token.NewFileSet()
42+
43+
src, err := os.ReadFile(srcFile)
44+
require.NoError(t, err)
45+
46+
f, err := parser.ParseFile(fset, srcFile, src, parser.ParseComments)
47+
require.NoError(t, err)
48+
49+
var valueNames []stringConstantNameAndValue
50+
51+
for _, decl := range f.Decls {
52+
if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.CONST {
53+
for _, spec := range gen.Specs {
54+
// Always ast.ValueSpec for token.CONST.
55+
valueSpec := spec.(*ast.ValueSpec) //nolint:forcetypeassert
56+
57+
typeIdent, ok := valueSpec.Type.(*ast.Ident)
58+
if !ok || typeIdent.Name != typeName {
59+
continue
60+
}
61+
62+
for i, nameIdent := range valueSpec.Names {
63+
// Force type assert because we expect one of our constants
64+
// to be defined as a basic type literal like this.
65+
basicLitExpr := valueSpec.Values[i].(*ast.BasicLit) //nolint:forcetypeassert
66+
67+
valueNames = append(valueNames, stringConstantNameAndValue{
68+
Name: nameIdent.Name,
69+
Value: basicLitExpr.Value[1 : len(basicLitExpr.Value)-1], // strip quote on either side
70+
})
71+
}
72+
}
73+
}
74+
}
75+
76+
if len(valueNames) < 1 {
77+
require.FailNow(t, "No values found", "No values found for source file and constant type: %s / %s", srcFile, typeName)
78+
}
79+
80+
return valueNames
81+
}

0 commit comments

Comments
 (0)