Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Commit

Permalink
Add method to clone client options object (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Yang authored May 16, 2017
1 parent 0a8ca92 commit 2a0a9ad
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<artifactId>cherami-client-java-parent-pom</artifactId>
<groupId>com.uber.cherami</groupId>
<version>3.0.1</version>
<version>3.0.2</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<artifactId>cherami-client-java-parent-pom</artifactId>
<groupId>com.uber.cherami</groupId>
<version>3.0.1</version>
<version>3.0.2</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
35 changes: 26 additions & 9 deletions lib/src/main/java/com/uber/cherami/client/ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ClientOptions {
/**
* The tChannel timeout in milliseconds
*/
private final long rpcTimeoutMillis;
private long rpcTimeoutMillis = DEFAULT_RPC_TIMEOUT_MILLIS;

/**
* Deployment string that gets added
Expand All @@ -45,23 +45,19 @@ public class ClientOptions {
* staging, then the cherami hyperbahn
* endpoint would be cherami-frontendhost_staging.
*/
private final String deploymentStr;
private String deploymentStr = "prod";

/**
* Name of the service using the cheramiClient.
*/
private final String clientAppName;
private String clientAppName = "unknown";

/**
* Client for metrics reporting.
*/
private final MetricsClient metricsClient;
private MetricsClient metricsClient = new DefaultMetricsClient();

private ClientOptions(Builder builder) {
this.rpcTimeoutMillis = builder.rpcTimeoutMillis;
this.deploymentStr = builder.deploymentStr;
this.clientAppName = builder.clientAppName;
this.metricsClient = builder.metricsClient;
private ClientOptions() {
}

/**
Expand All @@ -81,6 +77,27 @@ public ClientOptions(long timeout, TimeUnit timeUnit) {
this(new Builder().setRpcTimeout(timeUnit.toMillis(timeout)));
}

private ClientOptions(Builder builder) {
this.rpcTimeoutMillis = builder.rpcTimeoutMillis;
this.deploymentStr = builder.deploymentStr;
this.clientAppName = builder.clientAppName;
this.metricsClient = builder.metricsClient;
}

/**
* Copy to another client options with a different metrics client to report metrics.
* @param metricsClient metrics client
* @return client options
*/
public ClientOptions copyWithMetricsClient(MetricsClient metricsClient) {
ClientOptions clone = new ClientOptions();
clone.rpcTimeoutMillis = this.rpcTimeoutMillis;
clone.deploymentStr = this.deploymentStr;
clone.clientAppName = this.clientAppName;
clone.metricsClient = metricsClient;
return clone;
}

/**
* @return
* Returns the rpc timeout value in millis.
Expand Down
69 changes: 69 additions & 0 deletions lib/src/test/java/com/uber/cherami/client/ClientOptionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2017 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*******************************************************************************/
package com.uber.cherami.client;

import com.uber.cherami.PutMessage;
import com.uber.cherami.client.metrics.DefaultMetricsClient;
import com.uber.cherami.client.metrics.MetricsClient;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Unit tests for generating and verifying ClientOptions
*/
public class ClientOptionsTest {
private final Logger logger = LoggerFactory.getLogger(ClientOptionsTest.class);
private final PutMessage testPutMessage;
private final ChecksumValidator checksumValidator;
private ChecksumWriter checksumWriter;
private CheramiDeliveryImpl testDelivery;

public ClientOptionsTest() {
this.testPutMessage = new PutMessage();
this.testPutMessage.setData("Hello, world!".getBytes());
this.checksumValidator = new ChecksumValidator();
}

@Test
public void testClientOptions() {
ClientOptions options = new ClientOptions.Builder()
.setClientAppName("client1")
.setDeploymentStr("staging")
.setRpcTimeout(123)
.setMetricsClient(null)
.build();
Assert.assertEquals("client1", options.getClientAppName());
Assert.assertEquals("staging", options.getDeploymentStr());
Assert.assertEquals(123L, options.getRpcTimeoutMillis());
Assert.assertEquals(null, options.getMetricsClient());

MetricsClient metricsClient = new DefaultMetricsClient();
options = options.copyWithMetricsClient(metricsClient);
Assert.assertEquals("client1", options.getClientAppName());
Assert.assertEquals("staging", options.getDeploymentStr());
Assert.assertEquals(123L, options.getRpcTimeoutMillis());
Assert.assertEquals(metricsClient, options.getMetricsClient());
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<groupId>com.uber.cherami</groupId>
<artifactId>cherami-client-java-parent-pom</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
<packaging>pom</packaging>

<name>cherami</name>
Expand Down

0 comments on commit 2a0a9ad

Please sign in to comment.