Skip to content

Commit

Permalink
Add rivertype.JobStates with full list of job states
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brandur committed Apr 14, 2024
1 parent 3c4f8ee commit 7b8e838
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 17 deletions.
2 changes: 1 addition & 1 deletion riverdriver/riverdatabasesql/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/lib/pq v1.10.9
github.com/riverqueue/river/riverdriver v0.2.0
github.com/riverqueue/river/rivertype v0.2.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.9.0
)

require (
Expand Down
11 changes: 2 additions & 9 deletions riverdriver/riverdatabasesql/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -17,18 +16,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion riverdriver/riverpgxv5/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/jackc/puddle/v2 v2.2.1
github.com/riverqueue/river/riverdriver v0.2.0
github.com/riverqueue/river/rivertype v0.2.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.9.0
)

require (
Expand Down
8 changes: 2 additions & 6 deletions riverdriver/riverpgxv5/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
Expand Down
8 changes: 8 additions & 0 deletions rivertype/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/riverqueue/river/rivertype

go 1.21.4

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions rivertype/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 13 additions & 0 deletions rivertype/river_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ const (
JobStateScheduled JobState = "scheduled"
)

// JobStates returns all possible job states.
func JobStates() []JobState {
return []JobState{
JobStateAvailable,
JobStateCancelled,
JobStateCompleted,
JobStateDiscarded,
JobStateRetryable,
JobStateRunning,
JobStateScheduled,
}
}

// AttemptError is an error from a single job attempt that failed due to an
// error or a panic.
type AttemptError struct {
Expand Down
81 changes: 81 additions & 0 deletions rivertype/river_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package rivertype_test

import (
"go/ast"
"go/parser"
"go/token"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/riverqueue/river/rivertype"
)

func TestJobStates(t *testing.T) {
t.Parallel()

jobStates := rivertype.JobStates()

// One easy check that doesn't require the source file reading below.
require.Contains(t, jobStates, rivertype.JobStateAvailable)

// Get all job state names from the corresponding source file and make sure
// they're included in JobStates. Helps check that we didn't add a new value
// but forgot to add it to the full list of constant values.
for _, nameAndValue := range allValuesForStringConstantType(t, "river_type.go", "JobState") {
t.Logf("Checking for job state: %s / %s", nameAndValue.Name, nameAndValue.Value)
require.Contains(t, jobStates, rivertype.JobState(nameAndValue.Value))
}
}

// stringConstantNameAndValue is a name and value for a string constant like
// `JobStateAvailable` + `available`.
type stringConstantNameAndValue struct{ Name, Value string }

// allValuesForStringConstantType reads a Go source file and looks for all
// values for the named string constant.
func allValuesForStringConstantType(t *testing.T, srcFile, typeName string) []stringConstantNameAndValue {
t.Helper()

fset := token.NewFileSet()

src, err := os.ReadFile(srcFile)
require.NoError(t, err)

f, err := parser.ParseFile(fset, srcFile, src, parser.ParseComments)
require.NoError(t, err)

var valueNames []stringConstantNameAndValue

for _, decl := range f.Decls {
if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.CONST {
for _, spec := range gen.Specs {
// Always ast.ValueSpec for token.CONST.
valueSpec := spec.(*ast.ValueSpec) //nolint:forcetypeassert

typeIdent, ok := valueSpec.Type.(*ast.Ident)
if !ok || typeIdent.Name != typeName {
continue
}

for i, nameIdent := range valueSpec.Names {
// Force type assert because we expect one of our constants
// to be defined as a basic type literal like this.
basicLitExpr := valueSpec.Values[i].(*ast.BasicLit) //nolint:forcetypeassert

valueNames = append(valueNames, stringConstantNameAndValue{
Name: nameIdent.Name,
Value: basicLitExpr.Value[1 : len(basicLitExpr.Value)-1], // strip quote on either side
})
}
}
}
}

if len(valueNames) < 1 {
require.FailNow(t, "Not values found", "No values found for source file and constant type: %s / %s", srcFile, typeName)
}

return valueNames
}

0 comments on commit 7b8e838

Please sign in to comment.