Skip to content

feat(coherence|memcached): use testcontainers-go in tests #1724

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mdelapenya
Copy link
Contributor

@mdelapenya mdelapenya commented May 1, 2025

  • feat(memcached): use testcontainers-go in tests
  • feat(coherence): use testcontainers-go in tests

Summary by CodeRabbit

  • Tests
    • Improved test reliability and isolation by running each test against a dedicated containerized instance of Coherence or Memcached.
    • Updated test setup and teardown to ensure proper cleanup and environment control for each test.
    • Refactored test helper functions for easier configuration and usage within tests.

@mdelapenya mdelapenya requested a review from a team as a code owner May 1, 2025 00:25
@mdelapenya mdelapenya requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team May 1, 2025 00:25
Copy link
Contributor

coderabbitai bot commented May 1, 2025

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (2)
  • .github/workflows/benchmark.yml is excluded by !**/*.yml
  • .github/workflows/test-coherence.yml is excluded by !**/*.yml

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The test suites for both the Coherence and Memcache storage packages were refactored to utilize dynamically created, containerized instances managed by the testcontainers-go library. New helper functions were introduced to launch and configure these containers per test, replacing previous global or static test store instances. All tests and benchmarks now instantiate their own isolated storage instances, ensuring proper cleanup and improved test isolation. The setup and teardown logic were updated accordingly, and any shared initialization code was removed.

Changes

File(s) Change Summary
coherence/coherence_test.go Refactored to use a containerized Coherence server per test via testcontainers-go; added newTestConfig and newTestStoreWithConfig helpers; removed global testStore and TestMain; updated all tests and benchmarks to create and close their own store instances.
memcache/memcache_test.go Refactored to use a containerized memcached instance per test via testcontainers-go; added newTestStore helper; removed global testStore and TestMain; updated all tests and benchmarks to create and close their own store instances.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Function
    participant Testcontainers as testcontainers-go
    participant Container as Storage Container
    participant Store as Storage Instance

    Test->>Testcontainers: Launch container (Coherence/Memcache)
    Testcontainers->>Container: Start and wait for readiness
    Container-->>Testcontainers: Ready with host/port
    Testcontainers-->>Test: Return host/port
    Test->>Store: Create Storage instance with container config
    Store-->>Test: Storage ready
    Test->>Store: Run test operations
    Test->>Store: Close storage (cleanup)
    Test->>Testcontainers: Terminate container
Loading

Possibly related issues

Possibly related PRs

Suggested labels

✏️ Feature

Suggested reviewers

  • sixcolors
  • efectn
  • gaby
  • ReneWerner87

Poem

In a warren of tests, containers now bloom,
Each test with its own, in a virtual room.
No more shared burrows, no more old mess,
Isolation and cleanup—our new bunny success!
With testcontainers hopping, the code feels just right,
Happy storage and memcache, all through the night.
🐇✨


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🔭 Outside diff range comments (1)
memcache/memcache_test.go (1)

108-115: 🛠️ Refactor suggestion

Test_Memcache_Set_Expiration never asserts the key expiry

The test sleeps for 1 s but does not verify that the value actually expired,
so it can pass even if expiration is broken.

 time.Sleep(1100 * time.Millisecond)
+
+// verify the item vanished after the TTL
+result, err := testStore.Get(key)
+require.NoError(t, err)
+require.Zero(t, len(result), "expected key to be expired after TTL")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b6d622b and 7ed83dd.

⛔ Files ignored due to path filters (6)
  • .github/workflows/benchmark.yml is excluded by !**/*.yml
  • .github/workflows/test-memcache.yml is excluded by !**/*.yml
  • coherence/go.mod is excluded by !**/*.mod
  • coherence/go.sum is excluded by !**/*.sum, !**/*.sum
  • memcache/go.mod is excluded by !**/*.mod
  • memcache/go.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (2)
  • coherence/coherence_test.go (14 hunks)
  • memcache/memcache_test.go (11 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: compare (coherence)
  • GitHub Check: Tests (1.24.x)
  • GitHub Check: Tests (1.23.x)
🔇 Additional comments (2)
memcache/memcache_test.go (2)

38-45: 🛠️ Refactor suggestion

Call require.NoError before using the container reference

CleanupContainer registers a t.Cleanup callback that dereferences c.
If err is non-nil, c could be nil, producing a panic inside the cleanup.
Swap the two lines to guarantee that c is valid before it is captured:

- testcontainers.CleanupContainer(t, c)
- require.NoError(t, err)
+ require.NoError(t, err)
+ testcontainers.CleanupContainer(t, c)
⛔ Skipped due to learnings
Learnt from: mdelapenya
PR: gofiber/storage#1665
File: cassandra/cassandra_test.go:35-38
Timestamp: 2025-04-20T23:52:03.362Z
Learning: In testcontainers-go, calling `testcontainers.CleanupContainer(t, c)` before checking the error from container creation is safe due to the implementation details of the library. The CleanupContainer function handles nil or partially initialized containers gracefully.

31-38:

❌ Incorrect review comment

Check the testcontainers API – Run might not exist (compile-time risk)

The call sequence assumes a helper named testcontainers.Run.
In current testcontainers-go releases the idiomatic entry points are either:

testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ … })

or

testcontainers.RunContainer(ctx, …)

Run (alone) does not exist in most versions and will fail to compile.
Please double-check the library version you vendored and switch to the corresponding helper to avoid a build break.

Example fix (for the modern builder API):

- c, err := testcontainers.Run(ctx,
-     img,
-     testcontainers.WithExposedPorts(memcachedPort),
-     testcontainers.WithWaitStrategy(
-         wait.ForListeningPort(memcachedPort),
-     ),
- )
+ c, err := testcontainers.RunContainer(ctx,
+     testcontainers.WithImage(img),
+     testcontainers.WithExposedPorts(memcachedPort),
+     testcontainers.WithWaitStrategy(wait.ForListeningPort(memcachedPort)),
+ )

🌐 Web query:

Does the Go testcontainers-go library export a function named Run in its latest releases?

💡 Result:

Based on the search results, yes, testcontainers-go does export a Run function in its latest releases. The Run function serves as a convenience function that creates a new container and starts it[5]. It's designed to be the main entrypoint for modules in the testcontainers-go ecosystem.

Function Signature and Usage

The Run function typically follows this signature:

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)

This function accepts three parameters:

  • A Go context (context.Context)
  • The Docker image to use as a string
  • A variadic argument for container customization options[9]

Status in Different Modules

The Run function is being standardized across the testcontainers-go ecosystem. According to the documentation, it's becoming the preferred entrypoint for module initialization, while the previously used StartContainer function is being deprecated[2].

For some GCloud-related modules (BigQuery, BigTable, Datastore, Firestore), the documentation indicates that the Run function is "Not available until the next release of testcontainers-go main"[9], suggesting that it's being actively implemented across all modules.

Implementation Details

When implementing modules for testcontainers-go, developers are instructed to:

  • Make sure a public Container type exists that embeds the testcontainers.Container type
  • Ensure a public Run function exists as the entrypoint to the module
  • Define container options leveraging the testcontainers.ContainerCustomizer interface[2]

The Run function initializes the container request with default values, processes any customization options, and returns the configured container instance along with any potential errors[2].

Citations:


No changes needed to testcontainers.Run usage

The testcontainers-go library does export a Run(ctx, img string, opts …ContainerCustomizer) convenience function as its primary entry point. Your existing call to testcontainers.Run is valid and will compile against current releases—no update to RunContainer or GenericContainer is required.

Likely an incorrect or invalid review comment.

@gaby gaby requested a review from Copilot May 1, 2025 05:24
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR integrates testcontainers-go into the test suites for both Memcached and Coherence, improving test isolation and reliability.

  • Refactored test helper functions to launch containerized instances for tests.
  • Updated CI/CD workflows with new environment variables and Go versions.
  • Enhanced test configuration for both Memcached and Coherence.

Reviewed Changes

Copilot reviewed 7 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
memcache/memcache_test.go Introduced a newTestStore function using testcontainers-go to launch and cleanup a container.
coherence/coherence_test.go Refactored test configuration and store setup to use testcontainers-go for containerized tests.
.github/workflows/test-memcache.yml Updated Go version matrix and set TEST_MEMCACHED_IMAGE environment variable for consistency.
.github/workflows/test-coherence.yml Adjusted environment variables to set TEST_COHERENCE_IMAGE and streamlined container usage.
.github/workflows/benchmark.yml Added environment variables for TEST_COHERENCE_IMAGE and TEST_MEMCACHED_IMAGE for benchmarks.
Files not reviewed (2)
  • coherence/go.mod: Language not supported
  • memcache/go.mod: Language not supported

@ReneWerner87
Copy link
Member

ReneWerner87 commented May 2, 2025

@mdelapenya
image

can you take a look

I restarted the bench, maybe it was flakyless

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants