This repository was archived by the owner on Sep 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Add additional tests #96
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package modusgraph_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| mg "github.com/hypermodeinc/modusgraph" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestClientPool(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| uri string | ||
| skip bool | ||
| }{ | ||
| { | ||
| name: "ClientPoolWithFileURI", | ||
| uri: "file://" + t.TempDir(), | ||
| }, | ||
| { | ||
| name: "ClientPoolWithDgraphURI", | ||
| uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"), | ||
| skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if tc.skip { | ||
| t.Skip("Skipping test as MODUSGRAPH_TEST_ADDR is not set") | ||
| } | ||
|
|
||
| // Create a client with pool size 10 | ||
| client, err := mg.NewClient(tc.uri, mg.WithPoolSize(10)) | ||
| require.NoError(t, err) | ||
| defer client.Close() | ||
|
|
||
| // Test concurrent client pool usage | ||
| const numWorkers = 20 | ||
| var wg sync.WaitGroup | ||
| var mu sync.Mutex | ||
| var clientCount int | ||
|
|
||
| for i := 0; i < numWorkers; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
|
|
||
| // Get a client from the pool | ||
| client, cleanup, err := client.DgraphClient() | ||
| require.NoError(t, err) | ||
| require.NotNil(t, client) | ||
| txn := client.NewReadOnlyTxn() | ||
| ctx := context.Background() | ||
| _, err = txn.Query(ctx, "query { q(func: uid(1)) { uid } }") | ||
| require.NoError(t, err) | ||
| err = txn.Discard(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify we got a valid Dgraph client | ||
| if client != nil { | ||
| mu.Lock() | ||
| clientCount++ | ||
| mu.Unlock() | ||
| } | ||
|
|
||
| // Clean up the client | ||
| cleanup() | ||
| }() | ||
| } | ||
|
|
||
| // Wait for all workers to complete | ||
| wg.Wait() | ||
|
|
||
| // Verify we got clients from the pool | ||
| require.GreaterOrEqual(t, clientCount, 1) | ||
|
|
||
| // Get a client before close | ||
| beforeClient, cleanupBefore, err := client.DgraphClient() | ||
| require.NoError(t, err) | ||
| require.NotNil(t, beforeClient) | ||
|
|
||
| // Close the client pool | ||
| client.Close() | ||
| time.Sleep(100 * time.Millisecond) // Give some time for cleanup | ||
|
|
||
| // Verify we can still get a new client after close (pool will create a new one) | ||
| afterClient, cleanupAfter, err := client.DgraphClient() | ||
| require.NoError(t, err) | ||
| require.NotNil(t, afterClient) | ||
|
|
||
| // Verify the client is actually new | ||
| require.NotEqual(t, fmt.Sprintf("%p", beforeClient), fmt.Sprintf("%p", afterClient)) | ||
|
|
||
| // Clean up the client | ||
| cleanupAfter() | ||
|
|
||
| // Also clean up the before client if it wasn't already closed | ||
| cleanupBefore() | ||
| }) | ||
| } | ||
|
|
||
| // Reset singleton at the end of the test to ensure the next test can start fresh | ||
| mg.ResetSingleton() | ||
| } | ||
|
|
||
| func TestClientPoolStress(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| uri string | ||
| skip bool | ||
| }{ | ||
| { | ||
| name: "ClientPoolStressWithFileURI", | ||
| uri: "file://" + t.TempDir(), | ||
| }, | ||
| { | ||
| name: "ClientPoolStressWithDgraphURI", | ||
| uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"), | ||
| skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if tc.skip { | ||
| t.Skip("Skipping test as MODUSGRAPH_TEST_ADDR is not set") | ||
| } | ||
|
|
||
| // Create a client with pool size 10 | ||
| client, err := mg.NewClient(tc.uri, mg.WithPoolSize(10)) | ||
| require.NoError(t, err) | ||
| defer func() { | ||
| client.Close() | ||
| }() | ||
|
|
||
| // Test concurrent client pool usage with high load | ||
| const numWorkers = 20 | ||
| const iterations = 10 | ||
| var wg sync.WaitGroup | ||
| var successCount int | ||
| var errorCount int | ||
| var mu sync.Mutex | ||
|
|
||
| for i := 0; i < numWorkers; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < iterations; j++ { | ||
| dgraphClient, cleanup, err := client.DgraphClient() | ||
| if err != nil { | ||
| mu.Lock() | ||
| errorCount++ | ||
| mu.Unlock() | ||
| continue | ||
| } | ||
|
|
||
| if dgraphClient != nil { | ||
| // Test the client works | ||
| txn := dgraphClient.NewReadOnlyTxn() | ||
| ctx := context.Background() | ||
| _, err = txn.Query(ctx, "query { q(func: uid(1)) { uid } }") | ||
| if err != nil { | ||
| err = txn.Discard(ctx) | ||
| if err != nil { | ||
| mu.Lock() | ||
| errorCount++ | ||
| mu.Unlock() | ||
| } | ||
| cleanup() | ||
| continue | ||
| } | ||
| err = txn.Discard(ctx) | ||
| if err != nil { | ||
| mu.Lock() | ||
| errorCount++ | ||
| mu.Unlock() | ||
| cleanup() | ||
| continue | ||
| } | ||
|
|
||
| mu.Lock() | ||
| successCount++ | ||
| mu.Unlock() | ||
| } | ||
|
|
||
| // Clean up the client | ||
| cleanup() | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| require.Greater(t, successCount, 0) | ||
| }) | ||
|
|
||
| mg.ResetSingleton() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.