@@ -62,6 +62,7 @@ public abstract class ServiceOptions<
6262 private final ServiceRpcFactory <ServiceRpcT , OptionsT > serviceRpcFactory ;
6363 private final int connectTimeout ;
6464 private final int readTimeout ;
65+ private final Clock clock ;
6566
6667 public interface HttpTransportFactory extends Serializable {
6768 HttpTransport create ();
@@ -91,7 +92,40 @@ public HttpTransport create() {
9192 }
9293 }
9394
95+ /**
96+ * A class providing access to the current time in milliseconds. This class is mainly used for
97+ * testing and will be replaced by Java8's {@code java.time.Clock}.
98+ *
99+ * Implementations should implement {@code Serializable} wherever possible and must document
100+ * whether or not they do support serialization.
101+ */
102+ public static abstract class Clock {
103+
104+ private static ServiceOptions .Clock DEFAULT_TIME_SOURCE = new DefaultClock ();
105+
106+ /**
107+ * Returns current time in milliseconds according to this clock.
108+ */
109+ public abstract long millis ();
110+
111+ /**
112+ * Returns the default clock. Default clock uses {@link System#currentTimeMillis()} to get time
113+ * in milliseconds.
114+ */
115+ public static ServiceOptions .Clock defaultClock () {
116+ return DEFAULT_TIME_SOURCE ;
117+ }
118+
119+ private static class DefaultClock extends ServiceOptions .Clock implements Serializable {
94120
121+ private static final long serialVersionUID = -5077300394286703864L ;
122+
123+ @ Override
124+ public long millis () {
125+ return System .currentTimeMillis ();
126+ }
127+ }
128+ }
95129
96130 protected abstract static class Builder <
97131 ServiceRpcT ,
@@ -106,6 +140,7 @@ protected abstract static class Builder<
106140 private ServiceRpcFactory <ServiceRpcT , OptionsT > serviceRpcFactory ;
107141 private int connectTimeout = -1 ;
108142 private int readTimeout = -1 ;
143+ private Clock clock ;
109144
110145 protected Builder () {}
111146
@@ -125,6 +160,18 @@ protected B self() {
125160 return (B ) this ;
126161 }
127162
163+ /**
164+ * Sets the service's clock. The clock is mainly used for testing purpose. {@link Clock} will be
165+ * replaced by Java8's {@code java.time.Clock}.
166+ *
167+ * @param clock the clock to set
168+ * @return the builder.
169+ */
170+ public B clock (Clock clock ) {
171+ this .clock = clock ;
172+ return self ();
173+ }
174+
128175 /**
129176 * Sets project id.
130177 *
@@ -221,6 +268,7 @@ protected ServiceOptions(Builder<ServiceRpcT, OptionsT, ?> builder) {
221268 serviceRpcFactory = builder .serviceRpcFactory ;
222269 connectTimeout = builder .connectTimeout ;
223270 readTimeout = builder .readTimeout ;
271+ clock = firstNonNull (builder .clock , Clock .defaultClock ());
224272 }
225273
226274 private static AuthCredentials defaultAuthCredentials () {
@@ -419,9 +467,17 @@ public int readTimeout() {
419467 return readTimeout ;
420468 }
421469
470+ /**
471+ * Returns the service's clock. Default time source uses {@link System#currentTimeMillis()} to
472+ * get current time.
473+ */
474+ public Clock clock () {
475+ return clock ;
476+ }
477+
422478 protected int baseHashCode () {
423479 return Objects .hash (projectId , host , httpTransportFactory , authCredentials , retryParams ,
424- serviceRpcFactory );
480+ serviceRpcFactory , connectTimeout , readTimeout , clock );
425481 }
426482
427483 protected boolean baseEquals (ServiceOptions <?, ?> other ) {
@@ -430,7 +486,10 @@ protected boolean baseEquals(ServiceOptions<?, ?> other) {
430486 && Objects .equals (httpTransportFactory , other .httpTransportFactory )
431487 && Objects .equals (authCredentials , other .authCredentials )
432488 && Objects .equals (retryParams , other .retryParams )
433- && Objects .equals (serviceRpcFactory , other .serviceRpcFactory );
489+ && Objects .equals (serviceRpcFactory , other .serviceRpcFactory )
490+ && Objects .equals (connectTimeout , other .connectTimeout )
491+ && Objects .equals (readTimeout , other .readTimeout )
492+ && Objects .equals (clock , clock );
434493 }
435494
436495 public abstract Builder <ServiceRpcT , OptionsT , ?> toBuilder ();
0 commit comments