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