Skip to content

Changes to better support random seeds for flaky tests. #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 1, 2025

Conversation

rsned
Copy link
Collaborator

@rsned rsned commented Mar 26, 2025

Add flag (--s2_random_seed) to specify the seed used for random generation in tests.
Add new testing flag to specify the s2 random seed to allow repeatability between test runs.
Refactor random test methods to allow optional seeded generators.
Fix up all the functions in s2_test.go to take an optional random generator.
Change the places that actually generate the random values to test for the optional parameter and use it if set.
Minor fixes to a couple tests to match the current logic and values in their corresponding C++ tests. (paddedcell_test to use uniform float vs random float, fractal test cases relative error size)
Update comments on random seeds to reference current rand form.
With the addition of the random seed flag in testing now, update the comments on the cases that were triggering flaky tests. If they still show flakes, the specific cases can now be pushed through to the underlying random calls.

Fixes #120

rsned added 7 commits March 25, 2025 11:29
Add new testing flag to specify the s2 random seed to allow repeatability between test runs.
Fix up all the functions in s2_test.go to take an optional random generator.
Change the places that actually generate the random values to test for the optional parameter and use it if set.
With the addition of the random seed flag in testing now, update the comments
on the cases that were triggering flaky tests. If they still show flakes,
the specific cases can now be pushed through to the underlying random calls.
s2/cell_test.go Outdated
@@ -560,8 +559,10 @@ func TestCellContainsPoint(t *testing.T) {

func TestCellContainsPointConsistentWithS2CellIDFromPoint(t *testing.T) {
// About 1% flaky with a random seed.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not from my limited random seed testing. If you want to try patching it and running a few thousand rounds as well and see if you can get it to trigger like before or not.

s2/s2_test.go Outdated

// To set in go testing add "--s2_random_seed=123" to your test command.
// When using blaze/bazel add "--test_arg=--s2_random_seed=123"
s2RandomSeed = flag.Int64("s2_random_seed", 1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a fixed seed seems bad, since then we're always testing the same seed. Can we have a default that means "pick a random seed"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically a non-fixed seed should be used, such as time.Now().UnixNano(). The closest option would be to try seeding it with a rand.Int63() from whatever the runtime chooses as a random value and then only if a specific seed is set, use that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds reasonable.

s2/s2_test.go Outdated
// set up a random generator for use in tests.
// Prior to Go 1.20, the generator was seeded like Seed(1) at program startup.
// Using the default seed flag value will produce the prior behavior.
random = rand.New(rand.NewSource(*s2RandomSeed))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this going to interact with test_filter? If there's a failure, we don't want it to go away when we change the test filter.

It seems we don't want one random source shared with all test cases, it should be re-initialized in each one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best we can do is create specific new ones when we think we really need it. Otherwise we would need to go into every single Test... function in every file and add the NewSource(...) line to each method to do this. For most tests that's overkill since they really don't care, they usually just need whatever random value comes next.

If we end up with a growth of flaky tests then we may need to go down that route, but for now I would prefer to just use the one (basically what it was before only explicitly using the shared one rather than relying on the shared one from package rand.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise we would need to go into every single Test... function in every file and add the NewSource(...) line to each method to do this. For most tests that's overkill since they really don't care, they usually just need whatever random value comes next.

This is what we did in C++. More flaky tests will almost certainly be found this way. It's a bigger project, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far it seems to have highlighted a couple more while I think I've tracked down what was wrong with a couple of the others.

rsned added 3 commits March 26, 2025 22:22
Drop repeated issue comments.
Replace static seed with a somewhat random seed.
Test if user set a seed and if so use that.
Fix a few more test bugs.
Update TODO comment assignee.
rsned and others added 4 commits March 31, 2025 11:50
Add TODO for a couple places where the helper differs from the corresponding C++ helpers.
Touch up a couple method comments.
TestCellUnionNormalizePseudoRandom less flaky than before but still somewhat, so set an explicit seed on it for now.
Add comments on more detected differences from C++ that will need to be synced up and resolved to maybe fix the flaky differences.
@rsned rsned merged commit 45267f9 into golang:master Apr 1, 2025
2 checks passed
abhinavdangeti added a commit to blevesearch/geo that referenced this pull request Apr 17, 2025
```
* 7807d88 Abhinav Dangeti | Add github workflows
* f0977fe Abhinav Dangeti | Reroute golang/geo imports to blevesearch/geo
*   d718384 Abhi Dangeti | Merge branch 'master' into golang_master
|\  
| * 6c403ec Abhi Dangeti | Upgrade to [email protected] (#17)
| * d029aee Abhi Dangeti | Move up to [email protected] (#16)
| * e7a63fe Abhi Dangeti | MB-54131: Corner cases when buffer not setup & formatting (#15)
| * 045f1ed Likith B | MB-54131: Geoshape query decode optimization (#14)
| * 7135870 Abhi Dangeti | Update GO version (#13)
| * c261b53 Abhi Dangeti | Upgrade to [email protected] (#12)
| * 7bbb772 Abhi Dangeti | Upgrade to [email protected] (#11)
| * a8f4556 Aditi Ahuja | Updated intersection logic (#10)
| * 256c704 Aditi Ahuja | Added disjoint relation (#8)
| * 1bc1d50 Aditi Ahuja | Fix for linestrings enclosed by polygon (#9)
| * 6c4acc9 Abhi Dangeti | Set bleve/bleve_index_api to v1.0.3 (#7)
| * c569765 Sreekanth Sivasankaran | Adding Value() ([]byte, error) method (#6)
| *   aab42ad Sreekanth Sivasankaran | Merge pull request #5 from blevesearch/vertex_linestring_fix
| |\  
| | * c1e383d Sreekanth Sivasankaran | Fix for Polygon-LineString containment
| |/  
| *   e435177 Sreekanth Sivasankaran | Merge pull request #4 from blevesearch/jsoniter_version
| |\  
| | * c8c7ced Sreekanth Sivasankaran | Reverting to a stable jsoniter version
| * | 1e74e8d Sreekanth Sivasankaran | Merge pull request #3 from blevesearch/polygon_hole_fix
| |\| 
| | * fa50322 Sreekanth Sivasankaran | Include the polygon vertices for WithIn query
| |/  
| *   4ff549e Sreekanth Sivasankaran | Merge pull request #2 from blevesearch/linestring_fix
| |\  
| | * d9ebb09 Sreekanth Sivasankaran | Updating LineString for multiple points.
| |/  
| *   0850958 Sreekanth Sivasankaran | Merge pull request #1 from blevesearch/s2_encode_decode
| |\  
| | * 4b50f5f Sreekanth Sivasankaran | Introduce Serialisation method for the geo shapes.
| |/  
| * 2b95922 Sreekanth Sivasankaran | Fix hasCrossingRelation() method
| * 5734244 Sreekanth Sivasankaran | updating repo name in go.mod
| * 2e12926 Sreekanth Sivasankaran | updating readme
| * f95441b Sreekanth Sivasankaran | adding region term indexer unit tests
| * 1daa5fa Sreekanth Sivasankaran | Introduce new APIs,
| * 187b978 Sreekanth Sivasankaran | adding commentary for region term indexing Options
| * 37c8f96 Sreekanth Sivasankaran | adding more commentary and utility functions
| * 0a702af Sreekanth Sivasankaran | Introducing CapFromCenterAndRadius api
| * ecc18e7 Sreekanth Sivasankaran | updating the go.mod files
| * 2112278 Sreekanth Sivasankaran | Update rect.go
| * 761d9da Sreekanth Sivasankaran | Update go.mod
| * c9f6529 Sreekanth Sivasankaran | cb build errors
| * c7de933 Sreekanth Sivasankaran | temp fix for marker terms
| * c708aa2 Sreekanth Sivasankaran | adding setter methods for points only and space optimisation
* | ce8b781 Robert Snedegar | Add golangci-lint Github Action formatter and lint checks (golang#140)
* | b0c6031 Robert Snedegar | Add the OSSF and other action badges to the top of the README (golang#149)
* | 97e19c1 Jesse Rosenstock | CellID.AllNeighbors: Return nil for invalid level (golang#146)
* | 9ff7231 Jesse Rosenstock | Run gofmt -s (golang#148)
* | 1bf973d Jesse Rosenstock | tests: Remove outdated flaky test comments (golang#147)
* | 07d601f Robert Snedegar | Create scorecard.yml for OSSF (golang#143)
* | b4895f7 Robert Snedegar | Fix some small issues that were flagged by upcoming lint checks. (golang#138)
* | b51e350 Pekka Vainio | edge_query: use shapeID instead of index.idForShape lookup (golang#136)
* | dc45a10 guoguangwu | lexicon: replace loop with values = append(values, ids...) (golang#102)
* | 7b99cb2 Robert Snedegar | Add methods for testing Points normalizability. (golang#127)
* | 35ab489 Peter Johnson | edge_distances: fix test (golang#106)
* | 45267f9 Robert Snedegar | Changes to better support random seeds for flaky tests. (golang#129)
* | 9f6155a cui fliter | query_options: remove redundant type conversion (golang#94)
* | b92cf6f Philipp Klose | Replace godoc.org links with pgk.go.dev (golang#93)
* | 0b6e08c Jonatas Emidio | cell_index_test: Fix typo (golang#125)
* | f57e2fe Sascha Brawer | metric_test: fix bad error message (golang#96)
* |   bc23e40 Robert Snedegar | Final two Lax types completing move to public types.
|\ \  
| * | 06402e1 Robert Snedegar | Converting remaining lax types to public.
| * | f404cd9 Robert Snedegar | Converting remaining lax types to public.
| * | 68235aa Robert Snedegar | Rename lax_loop_test_test.go as final step of making laxLoop an official public type.   (Step 1 was lax_loop_test.go -> lax_loop.go with type laxLoop converting to public type LaxLoop.)   (Step 2 was remove lax_loop_test.go to make way for step 3)   (Step 3 lax_loop_test_test.go -> lax_loop_test.go to fit traditional conventions.)
| * | 9eb1fa2 Robert Snedegar | Remove lax_loop_test.go as step two of making laxLoop an official public type.
* | | 824710a Robert Snedegar | Merge pull request golang#123 from rsned/master
|\| | 
| * | 8e0a9ee Robert Snedegar | Restore check on childPos to no longer test for unreachable negative values. Fix two lint errors.
|/ /  
* | 2bb09a9 Robert Snedegar | Add IJCoordOfEdge, UVCoordOfEdge, CellBoundary and assosciated tests. Update comments in Cell. Migrate IJ and ST methods from Cell to stuv.go to more closely resemble C++ file splits. Add missing tests to stuv.go Add new maxXYZToUVError constant Add more tests for InterpolateDistance. Update comments. Add special case in interpolateFloat64. Add SignDotProd and submethods along with associated tests. Add gappa proof comments to predicates for some of the methods. Fix TestCellDistanceToEdge with updated tolerances to reduce test flakiness. (It is now better but still not zero random edges that fail. Still need more digging to resolve.) Initial internal components of polyline_alignment. Implement VertexAlignment and associated tests for PolylineAlignment Add EdgeRaw, VertexRaw to Cell for the unnormalized variations which are needed for some methods in s2 Predicates. Add SignDotProt and CircleEdgeIntersectionOrdering and associated sub methods and tests. Update textformat to support higher precision printout of values for higher fidelity save/load of values. Update related callers and add test to show the change. Update edge_distance with PointOnRay, PointOnLeft, PointOnRight, PointOnLine and related tests. Added commented parts of several existing tests that needed those. Move epsilon to tests since it's not used in the regular s2 code. Add roundingEpsilon and epsilonForDigits and related tests. Use a constant for sqrt(3) instead of recomputing it many many times.
* | 3c33606 Robert Snedegar | Revise the total precision bits of the big.Floats to be closer to the amounts used in C++ exact_float (from billions down to millions)
* |   438f30b Robert Snedegar | Merge pull request golang#122 from jmr/deflake
|\ \  
| * | a681026 Jesse Rosenstock | Deflake tests
|/ /  
* |   69a8a98 Robert Snedegar | Merge pull request golang#117 from jmr/polyline-interp-test
|\ \  
| * | 65bb56d Jesse Rosenstock | TestPolylineInterpolate: Fix unused `want` var
* | |   f13cb10 Robert Snedegar | Merge pull request golang#118 from jmr/cell-id-from-string
|\ \ \  
| * | | 070ebca Jesse Rosenstock | CellIDFromString: Remove no-op byte < 0 check
| |/ /  
* | | 1e303e5 Robert Snedegar | Update go.yml with correct branch name
* | |   cab189c Robert Snedegar | Merge pull request golang#119 from jmr/cell-test
|\ \ \  
| * | | 911c8a2 Jesse Rosenstock | s2cell_test: Increase TestCellDistanceToEdge tolerance
| |/ /  
* | |   ed1c8b9 Robert Snedegar | Merge pull request golang#116 from jmr/shapeutil-test
|\ \ \  
| |/ /  
|/| |   
| * | 3a1f0cb Jesse Rosenstock | TestShapeutilRangeIteratorNext: Test sentinel id
|/ /  
* |   6188549 Robert Snedegar | Merge pull request golang#114 from jmr/latlng-bm
|\ \  
| * | c5e5a69 Jesse Rosenstock | latlng_test: Add benchmark functions
* | |   2fc3d43 Robert Snedegar | Merge pull request golang#115 from golang/rsned-patch-1
|\ \ \  
| * | | ae3ea9d Robert Snedegar | Create go.yml
|/ / /  
* / / aa6d65d Robert Snedegar | Add go.sum to match go.mod.
|/ /  
* |   bd601d2 Robert Snedegar | Merge pull request golang#113 from jmr/comments
|\ \  
| * | 9c70ae5 Jesse Rosenstock | Add and reformat comments
|/ /  
* |   29de3e1 Robert Snedegar | Merge pull request golang#112 from jmr/update-2025-03-17
|\ \  
| * | 029bad8 Jesse Rosenstock | Export latest google3 version
|/ /  
* | 6adc566 Robert Snedegar | Fix package on s2intersect_test.go
* |   c62752d Robert Snedegar | Merge pull request golang#101 from rahul2393/master
|\ \  
| * | eee7799 Robert Snedegar | Sync to latest
|/ /  
* / c4acd7a Robert Snedegar | Update go version to support the use of 'any'.q
|/ 
```

---------

Signed-off-by: cui fliter <[email protected]>
Co-authored-by: Robert Snedegar <[email protected]>
Co-authored-by: Robert Snedegar <[email protected]>
Co-authored-by: Jesse Rosenstock <[email protected]>
Co-authored-by: Sascha Brawer <[email protected]>
Co-authored-by: Jonatas Emidio <[email protected]>
Co-authored-by: Philipp Klose <[email protected]>
Co-authored-by: cui fliter <[email protected]>
Co-authored-by: Peter Johnson <[email protected]>
Co-authored-by: guoguangwu <[email protected]>
Co-authored-by: Pekka Vainio <[email protected]>
Co-authored-by: Alan Strohm <[email protected]>
Co-authored-by: Alan Strohm <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

flaky tests
2 participants