-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Resolve #487: Enhance tests and fix linting issues #491
base: master
Are you sure you want to change the base?
Conversation
- Add randomized fields to Engine structs in tests - Implement cryptographically secure random utility functions - Address golangci-lint warnings
Codecov Report
@@ Coverage Diff @@
## master #491 +/- ##
==========================================
+ Coverage 97.30% 97.58% +0.28%
==========================================
Files 18 19 +1
Lines 780 871 +91
==========================================
+ Hits 759 850 +91
Misses 15 15
Partials 6 6
|
Thank you for the PR! |
- Avoid big package - Use strings.Builder for efficient string concatenation - Set default string length to 10 - Handle errors from rand.Read() Resolves: sashabaranov#487
- Memory efficient solution for RandomInt func - Improve error handling by logging them vice returning the type - Use RandomInt() instead Resolves: sashabaranov#487
- Add a seed for the random number generator to ensure that the tests are deterministic. - Move the expected engine outside of the handler - Add logic to compare the ID of the engine object returned by the client with the expected engine object.
} | ||
|
||
// TestListEngines Tests the list engines endpoint of the API using the mocked server. | ||
func TestListEngines(t *testing.T) { | ||
test.Seed(42) // Seed the RNG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the updates, that looks perfect! Could we keep seeding logic out for now, I think it would be useful in cases we'll actually need to reproduce something (e.g. fuzzing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your review, @sashabaranov.
I've can go ahead and remove the RNG seeding from TestListEngines()
as you suggested. I agree that there could be specific scenarios like fuzz testing where non-deterministic behavior is desirable.
// Possible compromise scenario though
We could introduce a flag or environment variable to decide whether or not to seed the RNG, allowing us to keep both deterministic and non-deterministic behavior depending on the use-case. Would that be an acceptable solution for future requirements?
Here's what I am thinking:
func MaybeSeedRNG() {
seed := os.Getenv("TEST_RNG_SEED")
if seedValue, err := strconv.ParseInt(seed, 10, 64); err == nil {
rand.Seed(seedValue)
return
}
// However, if the environment variable is set to "random",
// we can use the current time as the seed
if seedEnv == "random" {
rand.Seed(time.Now().UnixNano())
}
}
With this setup, we can control the RNG seeding behavior through the TEST_RNG_SEED
environment variable:
So to make the tests deterministic with a specific seed, we can set the environment variable to that seed (e.g., TEST_RNG_SEED=42
). To make tests non-deterministic, we can set the environment variable to "random" (TEST_RNG_SEED=random
).
If we don't set the environment variable, the RNG will not be seeded, maintaining its default non-deterministic behavior.
Then, we could call it whenever we want determinism, e.g:
func TestListEngines(t *testing.T) {
test.MaybeSeedRNG() // Call it here
client, server, teardown := setupOpenAITestServer()
defer teardown()
// ... rest of the function remains the same ...
}
Please advise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ealvar3z I like the MaybeSeedRNG
approach! Let's factor it out to the separate PR, trying to keep the PR size small-ish
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. See PR #509
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, let's remove test.Seed(42)
from this PR and merge!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ealvar3z it's not removed :D
checks.NoError(t, err, "GetEngine error") | ||
|
||
// Compare the two using only one field per code review comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we please remove these comments? They are relevant to review, but not for the codebase going forward
// TestGetEngine Tests the retrieve engine endpoint of the API using the mocked server. | ||
func TestGetEngine(t *testing.T) { | ||
client, server, teardown := setupOpenAITestServer() | ||
defer teardown() | ||
|
||
expectedEngine := RandomEngine() // move outside of handler per code review comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(2) Could we please remove these comments? They are relevant to review but not for the codebase going forward
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. Thank you for your patience.
Describe the change
Describe your solution
Tests
internal/test/random.go
). So in the end we'll get something like:Additional context
Issue: #487