@Override public V get(K key) { try { return getAsync(key).get(); } catch (Exception e) { throw new SamzaException(e); } }
@Test public void testGetWithCallbackExecutor() throws Exception { TableReadFunction<String, String> readFn = mock(TableReadFunction.class); // Sync is backed by async so needs to mock the async method doReturn(CompletableFuture.completedFuture("bar")).when(readFn).getAsync(anyString()); RemoteTable<String, String> table = getTable("testGetWithCallbackExecutor", readFn, null, Executors.newSingleThreadExecutor(), false); Thread testThread = Thread.currentThread(); table.getAsync("foo").thenAccept(result -> { Assert.assertEquals("bar", result); // Must be executed on the executor thread Assert.assertNotSame(testThread, Thread.currentThread()); }); } }
@Test public void testGetMultipleTables() { TableReadFunction<String, String> readFn1 = mock(TableReadFunction.class); TableReadFunction<String, String> readFn2 = mock(TableReadFunction.class); // Sync is backed by async so needs to mock the async method doReturn(CompletableFuture.completedFuture("bar1")).when(readFn1).getAsync(anyString()); doReturn(CompletableFuture.completedFuture("bar2")).when(readFn1).getAsync(anyString()); RemoteTable<String, String> table1 = getTable("testGetMultipleTables-1", readFn1, null, false); RemoteTable<String, String> table2 = getTable("testGetMultipleTables-2", readFn2, null, false); CompletableFuture<String> future1 = table1.getAsync("foo1"); CompletableFuture<String> future2 = table2.getAsync("foo2"); CompletableFuture.allOf(future1, future2) .thenAccept(u -> { Assert.assertEquals(future1.join(), "bar1"); Assert.assertEquals(future2.join(), "bar1"); }); }
private void doTestGet(boolean sync, boolean error, boolean retry) throws Exception { String tableId = "testGet-" + sync + error + retry; TableReadFunction<String, String> readFn = mock(TableReadFunction.class); // Sync is backed by async so needs to mock the async method CompletableFuture<String> future; if (error) { future = new CompletableFuture(); future.completeExceptionally(new RuntimeException("Test exception")); if (!retry) { doReturn(future).when(readFn).getAsync(anyString()); } else { final int [] times = new int[] {0}; doAnswer(args -> times[0]++ == 0 ? future : CompletableFuture.completedFuture("bar")) .when(readFn).getAsync(anyString()); } } else { future = CompletableFuture.completedFuture("bar"); doReturn(future).when(readFn).getAsync(anyString()); } if (retry) { doReturn(true).when(readFn).isRetriable(any()); } RemoteTable<String, String> table = getTable(tableId, readFn, null, retry); Assert.assertEquals("bar", sync ? table.get("foo") : table.getAsync("foo").get()); verify(table.readRateLimiter, times(error && retry ? 2 : 1)).throttle(anyString()); }