public final class NetworkBehavior extends Object
This class models three properties of a network:
IOException).MockRetrofit. Behavior can also
be applied elsewhere using calculateDelay(TimeUnit) and calculateIsFailure().
By default, instances of this class will use a 2 second delay with 40% variance and failures will occur 3% of the time.
| Modifier and Type | Class and Description |
|---|---|
static interface |
NetworkBehavior.Adapter<T>
Applies
NetworkBehavior to instances of T. |
| Modifier and Type | Method and Description |
|---|---|
long |
calculateDelay(TimeUnit unit)
Get the delay that should be used for delaying a response in accordance with configured
behavior.
|
boolean |
calculateIsFailure()
Randomly determine whether this call should result in a network failure in accordance with
configured behavior.
|
static NetworkBehavior |
create()
Create an instance with default behavior.
|
static NetworkBehavior |
create(Random random)
Create an instance with default behavior which uses
random to control variance and
failure calculation. |
long |
delay(TimeUnit unit)
The network round trip delay.
|
Throwable |
failureException()
The exception to be used when a failure is triggered.
|
int |
failurePercent()
The percentage of calls to
calculateIsFailure() that return true. |
void |
setDelay(long amount,
TimeUnit unit)
Set the network round trip delay.
|
void |
setFailureException(Throwable t)
Set the exception to be used when a failure is triggered.
|
void |
setFailurePercent(int failurePercent)
Set the percentage of calls to
calculateIsFailure() that return true. |
void |
setVariancePercent(int variancePercent)
Set the plus-or-minus variance percentage of the network round trip delay.
|
int |
variancePercent()
The plus-or-minus variance percentage of the network round trip delay.
|
public static NetworkBehavior create()
public static NetworkBehavior create(Random random)
random to control variance and
failure calculation.public void setDelay(long amount,
TimeUnit unit)
public long delay(TimeUnit unit)
public void setVariancePercent(int variancePercent)
public int variancePercent()
public void setFailurePercent(int failurePercent)
calculateIsFailure() that return true.public int failurePercent()
calculateIsFailure() that return true.public void setFailureException(Throwable t)
public Throwable failureException()
public boolean calculateIsFailure()
failureException() should be thrown.public long calculateDelay(TimeUnit unit)
Copyright © 2015 Square, Inc.. All Rights Reserved.