This commit is contained in:
Lucio Lelii 2022-07-26 15:14:18 +02:00
parent 966475516a
commit 438b928b7d
1 changed files with 71 additions and 81 deletions

View File

@ -37,16 +37,16 @@ import org.mockito.stubbing.Answer;
public class AsyncDelegateTest {
AsyncProxyDelegate<Object> delegate;
@Mock ProxyPlugin<Object,Object,?> plugin;
@Mock Object endpoint;
@Mock Call<Object,Object> call;
@Mock Object value;
@Mock Exception original;
@Mock Exception converted;
@Before
@SuppressWarnings("all")
public void setup() throws Exception {
@ -54,34 +54,34 @@ public class AsyncDelegateTest {
//create subject-under-testing
delegate =new AsyncProxyDelegate<Object>(mockDelegate(plugin,endpoint));
//common configuration staging: mocking a delegate is not that immediate..
when(plugin.name()).thenReturn("some service");
when(plugin.convert(original,delegate.config())).thenReturn(converted);
}
@Test
public void asyncCallsReturnFutureValues() throws Exception {
//stage call
when(call.call(endpoint)).thenReturn(value);
Future<Object> future = delegate.makeAsync(call);
Object output = future.get();
assertEquals(value,output);
assertFalse(future.isCancelled());
assertTrue(future.isDone());
}
@Test
public void asyncCallsTimeout() throws Exception {
//stage call
Answer<?> slowly = new Answer<Object>() {
@Override
@ -91,53 +91,48 @@ public class AsyncDelegateTest {
}
};
when(call.call(endpoint)).thenAnswer(slowly);
Future<Object> future = delegate.makeAsync(call);
try {
future.get(100,TimeUnit.MILLISECONDS);
fail();
}
catch(TimeoutException e) {}
}
@Test
public void asyncCallsExecuteInCallScope() throws Exception {
final String scope = "a/b/c";
SecretManagerProvider.instance.set(new Secret() {
@Override
public boolean isRefreshable() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isExpired() {
// TODO Auto-generated method stub
return false;
}
@Override
public Owner getOwner() {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, String> getHTTPAuthorizationHeaders() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getContext() {
return scope;
}
});
//stage call
Answer<?> checkingScope= new Answer<Object>() {
@Override
@ -146,22 +141,22 @@ public class AsyncDelegateTest {
return value;
}
};
when(call.call(endpoint)).thenAnswer(checkingScope);
Future<Object> future = delegate.makeAsync(call);
future.get();
}
@Test
public void asyncCallReturnConvertedFaultsAsInnerCauses() throws Exception {
//stage call
when(call.call(endpoint)).thenThrow(original);
Future<Object> future = delegate.makeAsync(call);
try {
future.get();
fail();
@ -169,45 +164,40 @@ public class AsyncDelegateTest {
catch(Exception fault) {
assertEquals(converted,fault.getCause());
}
}
@Test
public void asyncCallsAreInterrupted() throws Exception {
final String scope = "a/b/c";
SecretManagerProvider.instance.set(new Secret() {
@Override
public boolean isRefreshable() {
// TODO Auto-generated method stub
return false;
}
SecretManagerProvider.instance.set(new Secret() {
@Override
public boolean isExpired() {
// TODO Auto-generated method stub
return false;
}
@Override
public Owner getOwner() {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, String> getHTTPAuthorizationHeaders() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getContext() {
return scope;
}
});
//stage call
Answer<?> slowly = new Answer<Object>() {
@Override
@ -222,12 +212,12 @@ SecretManagerProvider.instance.set(new Secret() {
new Thread() {
public void run() {
future.cancel(true);
};
}.start();
try {
future.get();
fail();
@ -235,12 +225,12 @@ SecretManagerProvider.instance.set(new Secret() {
catch(CancellationException fault) {
assertTrue(future.isCancelled());
}
}
@Test
public void callbacksGetResults() throws Exception {
//stage call
Answer<?> answer = new Answer<Object>() {
@Override
@ -248,9 +238,9 @@ SecretManagerProvider.instance.set(new Secret() {
return value;
}
};
when(call.call(endpoint)).thenAnswer(answer);
@SuppressWarnings("all")
Callback<Object> callback = mock(Callback.class);
@ -258,16 +248,16 @@ SecretManagerProvider.instance.set(new Secret() {
//make sure the callback has had time to arrive
Thread.sleep(400);
verify(callback).done(value);
assertTrue(future.isDone());
}
@Test
public void callbacksGetTimeoutErrors() throws Exception {
//stage call
Answer<?> slowly = new Answer<Object>() {
@Override
@ -276,15 +266,15 @@ SecretManagerProvider.instance.set(new Secret() {
return value;
}
};
when(call.call(endpoint)).thenAnswer(slowly);
@SuppressWarnings("all")
Callback<Object> callback = mock(Callback.class);
when(callback.timeout()).thenReturn(100L);
final CountDownLatch latch = new CountDownLatch(1);
Answer<?> unblock = new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
@ -292,27 +282,27 @@ SecretManagerProvider.instance.set(new Secret() {
return value;
}
};
doAnswer(unblock).when(callback).onFailure(any(TimeoutException.class));
Future<?> future = delegate.makeAsync(call,callback);
//makes sure callback has been invoked
latch.await(5,SECONDS);
assertTrue(future.isCancelled());
}
@Test
public void callbacksGetFaults() throws Exception {
when(call.call(endpoint)).thenThrow(original);
@SuppressWarnings("all")
Callback<Object> callback = mock(Callback.class);
final CountDownLatch latch = new CountDownLatch(1);
Answer<?> unblock = new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
@ -323,14 +313,14 @@ SecretManagerProvider.instance.set(new Secret() {
return null;
}
};
doAnswer(unblock).when(callback).onFailure(any(Throwable.class));
delegate.makeAsync(call,callback);
//makes sure callback has been invoked
latch.await(1,SECONDS);
}
}