public final class NetworkBehavior
extends java.lang.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. Failures will occur 3% of the time. HTTP errors will occur 0% of the time.
Modifier and Type | Method and Description |
---|---|
long |
calculateDelay(java.util.concurrent.TimeUnit unit)
Get the delay that should be used for delaying a response in accordance with configured
behavior.
|
boolean |
calculateIsError()
Randomly determine whether this call should result in an HTTP error 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(java.util.Random random)
Create an instance with default behavior which uses
random to control variance and
failure calculation. |
retrofit2.Response<?> |
createErrorResponse()
The HTTP error to be used when an error is triggered.
|
long |
delay(java.util.concurrent.TimeUnit unit)
The network round trip delay.
|
int |
errorPercent()
The percentage of calls to
calculateIsError() that return true . |
java.lang.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,
java.util.concurrent.TimeUnit unit)
Set the network round trip delay.
|
void |
setErrorFactory(java.util.concurrent.Callable<retrofit2.Response<?>> errorFactory)
Set the error response factory to be used when an error is triggered.
|
void |
setErrorPercent(int errorPercent)
Set the percentage of calls to
calculateIsError() that return true . |
void |
setFailureException(java.lang.Throwable exception)
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(java.util.Random random)
random
to control variance and
failure calculation.public void setDelay(long amount, java.util.concurrent.TimeUnit unit)
public long delay(java.util.concurrent.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(java.lang.Throwable exception)
It is a best practice to remove the stack trace from exception
since it can
misleadingly point to code unrelated to this class.
public java.lang.Throwable failureException()
public int errorPercent()
calculateIsError()
that return true
.public void setErrorPercent(int errorPercent)
calculateIsError()
that return true
.public void setErrorFactory(java.util.concurrent.Callable<retrofit2.Response<?>> errorFactory)
Response.isSuccessful()
returns false.public retrofit2.Response<?> createErrorResponse()
public boolean calculateIsFailure()
failureException()
should be thrown.public boolean calculateIsError()
createErrorResponse()
should be returned.public long calculateDelay(java.util.concurrent.TimeUnit unit)