Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ealvar3z
Copy link
Contributor

Describe the change

  • Add randomized fields to Engine structs in tests

Describe your solution

  • Implement cryptographically secure random utility functions

Tests

  • Make these structs non-empty, by randomizing them, while keeping randomization logic in the internal/test directory (internal/test/random.go). So in the end we'll get something like:
server.RegisterHandler("/v1/engines/text-davinci-003", func(w http.ResponseWriter, r *http.Request) {
	resBytes, _ := json.Marshal(Engine{ID: test.randomString(), Object: test.randomString()})
	fmt.Fprintln(w, string(resBytes))
})
  • Address golangci-lint warnings

Additional context

  • None

Issue: #487

- Add randomized fields to Engine structs in tests
- Implement cryptographically secure random utility functions
- Address golangci-lint warnings
@codecov
Copy link

codecov bot commented Sep 12, 2023

Codecov Report

Merging #491 (5341647) into master (3589837) will increase coverage by 0.28%.
Report is 7 commits behind head on master.
The diff coverage is 100.00%.

@@            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              
Files Coverage Δ
embeddings.go 100.00% <100.00%> (ø)

... and 14 files with indirect coverage changes

@sashabaranov
Copy link
Owner

Thank you for the PR!

engines_test.go Outdated Show resolved Hide resolved
- 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
engines_test.go Outdated Show resolved Hide resolved
- 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
Copy link
Owner

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)

Copy link
Contributor Author

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.

Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. See PR #509

Copy link
Owner

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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

Copy link
Contributor Author

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.

@sashabaranov sashabaranov mentioned this pull request Nov 12, 2023
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.

None yet

2 participants