-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rolling strategy support to RPC clients
Implemented new rolling strategy interfaces and updated RPC clients to utilize the rolling strategies. Introduced two strategies: RoundRobin and AlwaysUseFirst, modifying existing tests to incorporate these changes.
- Loading branch information
Showing
5 changed files
with
161 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,51 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type rollClient struct { | ||
callCount int | ||
name string | ||
} | ||
|
||
func TestRollingStrategy(t *testing.T) { | ||
|
||
rollingStrategy := NewRollingStrategyRoundRobin[*rollClient]() | ||
rollingStrategy.reset() | ||
|
||
clients := NewClients(2*time.Second, rollingStrategy) | ||
clients.Add(&rollClient{name: "c.1"}) | ||
clients.Add(&rollClient{name: "c.2"}) | ||
clients.Add(&rollClient{name: "c.3"}) | ||
clients.Add(&rollClient{name: "c.a"}) | ||
clients.Add(&rollClient{name: "c.b"}) | ||
|
||
var clientNames []string | ||
_, err := WithClients(clients, func(ctx context.Context, client *rollClient) (v any, err error) { | ||
clientNames = append(clientNames, client.name) | ||
if client.name == "c.3" { | ||
return nil, nil | ||
} | ||
|
||
return nil, fmt.Errorf("next please") | ||
}) | ||
|
||
require.NoError(t, err) | ||
//require.ErrorIs(t, err, ErrorNoMoreClient) | ||
require.Equal(t, []string{"c.1", "c.2", "c.3"}, clientNames) | ||
|
||
_, err = WithClients(clients, func(ctx context.Context, client *rollClient) (v any, err error) { | ||
clientNames = append(clientNames, client.name) | ||
return nil, fmt.Errorf("next please") | ||
}) | ||
|
||
require.ErrorIs(t, err, ErrorNoMoreClient) | ||
require.Equal(t, []string{"c.1", "c.2", "c.3", "c.3", "c.a", "c.b", "c.1", "c.2"}, clientNames) | ||
|
||
} |
This file contains 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