-
Notifications
You must be signed in to change notification settings - Fork 16
Add DurableTaskGrpcClientFactory
#256
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
Changes from all commits
af16d08
9ef0448
82a3bda
ef2048b
fc9cfcf
ae5d802
39374b0
e34a7f9
29055a2
96e02b7
8377269
b30d6ca
1dc95d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.durabletask; | ||
|
|
||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| public final class DurableTaskGrpcClientFactory { | ||
| private static final ConcurrentMap<Integer, DurableTaskGrpcClient> portToClientMap = new ConcurrentHashMap<>(); | ||
|
|
||
| // Private to prevent instantiation and enforce a singleton pattern | ||
| private DurableTaskGrpcClientFactory() { | ||
| } | ||
|
|
||
| public static DurableTaskClient getClient(int port, String defaultVersion) { | ||
| return portToClientMap.computeIfAbsent(port, p -> new DurableTaskGrpcClient(p, defaultVersion)); | ||
| } | ||
| } | ||
sophiatev marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
9
to
19
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.durabletask; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| /** | ||
| * Unit tests for DurableTaskGrpcClientFactory. | ||
| */ | ||
| public class DurableTaskGrpcClientFactoryTest { | ||
|
|
||
| private static final String DEFAULT_VERSION = null; | ||
|
|
||
| @Test | ||
| void getClient_samePort_returnsSameInstance() { | ||
| // Arrange | ||
| int port = 5001; | ||
|
|
||
| // Act | ||
| DurableTaskClient client1 = DurableTaskGrpcClientFactory.getClient(port, DEFAULT_VERSION); | ||
| DurableTaskClient client2 = DurableTaskGrpcClientFactory.getClient(port, DEFAULT_VERSION); | ||
|
|
||
| // Assert | ||
| assertNotNull(client1, "First client should not be null"); | ||
| assertNotNull(client2, "Second client should not be null"); | ||
| assertSame(client1, client2, "getClient should return the same instance for the same port"); | ||
| } | ||
|
|
||
| @Test | ||
| void getClient_differentPorts_returnsDifferentInstances() { | ||
| // Arrange | ||
| int port1 = 5002; | ||
| int port2 = 5003; | ||
|
|
||
| // Act | ||
| DurableTaskClient client1 = DurableTaskGrpcClientFactory.getClient(port1, DEFAULT_VERSION); | ||
| DurableTaskClient client2 = DurableTaskGrpcClientFactory.getClient(port2, DEFAULT_VERSION); | ||
|
|
||
| // Assert | ||
| assertNotNull(client1, "Client for port1 should not be null"); | ||
| assertNotNull(client2, "Client for port2 should not be null"); | ||
| assertNotSame(client1, client2, "getClient should return different instances for different ports"); | ||
| } | ||
|
|
||
| @Test | ||
| void getClient_multiplePorts_maintainsCorrectMapping() { | ||
| // Arrange | ||
| int port1 = 5004; | ||
| int port2 = 5005; | ||
| int port3 = 5006; | ||
|
|
||
| // Act | ||
| DurableTaskClient client1 = DurableTaskGrpcClientFactory.getClient(port1, DEFAULT_VERSION); | ||
| DurableTaskClient client2 = DurableTaskGrpcClientFactory.getClient(port2, DEFAULT_VERSION); | ||
| DurableTaskClient client3 = DurableTaskGrpcClientFactory.getClient(port3, DEFAULT_VERSION); | ||
|
|
||
| // Request the same ports again | ||
| DurableTaskClient client1Again = DurableTaskGrpcClientFactory.getClient(port1, DEFAULT_VERSION); | ||
| DurableTaskClient client2Again = DurableTaskGrpcClientFactory.getClient(port2, DEFAULT_VERSION); | ||
| DurableTaskClient client3Again = DurableTaskGrpcClientFactory.getClient(port3, DEFAULT_VERSION); | ||
|
|
||
| // Assert | ||
| // Verify each port returns the same instance | ||
| assertSame(client1, client1Again, "Port " + port1 + " should return the same instance"); | ||
| assertSame(client2, client2Again, "Port " + port2 + " should return the same instance"); | ||
| assertSame(client3, client3Again, "Port " + port3 + " should return the same instance"); | ||
|
|
||
| // Verify all instances are different from each other | ||
| assertNotSame(client1, client2, "Client for port1 and port2 should be different"); | ||
| assertNotSame(client1, client3, "Client for port1 and port3 should be different"); | ||
| assertNotSame(client2, client3, "Client for port2 and port3 should be different"); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.