|
20 | 20 | import static com.google.cloud.bigtable.misc_utilities.AuthorizedViewTestHelper.createTestAuthorizedView; |
21 | 21 | import static com.google.common.truth.Truth.assertThat; |
22 | 22 | import static com.google.common.truth.TruthJUnit.assume; |
| 23 | +import static org.junit.Assert.assertEquals; |
23 | 24 | import static org.junit.Assert.fail; |
24 | 25 |
|
25 | 26 | import com.google.api.gax.rpc.PermissionDeniedException; |
|
31 | 32 | import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv; |
32 | 33 | import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; |
33 | 34 | import com.google.protobuf.ByteString; |
| 35 | + |
| 36 | +import java.math.BigInteger; |
34 | 37 | import java.util.UUID; |
35 | 38 | import java.util.concurrent.TimeUnit; |
36 | 39 | import org.junit.ClassRule; |
|
40 | 43 |
|
41 | 44 | @RunWith(JUnit4.class) |
42 | 45 | public class MutateRowIT { |
| 46 | + static { |
| 47 | + System.setProperty("bigtable.env", "cloud"); |
| 48 | + System.setProperty("bigtable.project", "google.com:cloud-bigtable-dev"); |
| 49 | + System.setProperty("bigtable.instance", "stepanian-agg"); |
| 50 | + System.setProperty("bigtable.table", "my-table"); |
| 51 | + System.setProperty("bigtable.data-endpoint", "test-bigtable.sandbox.googleapis.com:443"); |
| 52 | + System.setProperty("bigtable.admin-endpoint", "test-bigtableadmin.sandbox.googleapis.com:443"); |
| 53 | + |
| 54 | +// -D -Dbigtable.data-endpoint=bigtable.googleapis.com -Dbigtable.admin-endpoint=test-bigtableadmin.sandbox.googleapis.com |
| 55 | + } |
43 | 56 | @ClassRule public static TestEnvRule testEnvRule = new TestEnvRule(); |
44 | 57 |
|
45 | 58 | @Test |
@@ -152,4 +165,58 @@ public void testOnAuthorizedView() throws Exception { |
152 | 165 | .getTableAdminClient() |
153 | 166 | .deleteAuthorizedView(testEnvRule.env().getTableId(), testAuthorizedView.getId()); |
154 | 167 | } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testIdempotentAggregates() throws Exception { |
| 171 | + String rowKey = UUID.randomUUID().toString(); |
| 172 | + // String familyId = testEnvRule.env().getFamilyId(); |
| 173 | + String familyId = "cfagg1"; |
| 174 | + |
| 175 | + // First mutation w/ idempotency token. |
| 176 | + testEnvRule |
| 177 | + .env() |
| 178 | + .getDataClient() |
| 179 | + .mutateRowAsync( |
| 180 | + RowMutation.create(testEnvRule.env().getTableId(), rowKey) |
| 181 | + .addToCell(familyId, "cq", 0, 10) |
| 182 | + .setIdempotency(ByteString.copyFromUtf8("abcdefgh")) |
| 183 | + ) |
| 184 | + .get(1, TimeUnit.MINUTES); |
| 185 | + |
| 186 | + // Now replay the mutation. |
| 187 | + testEnvRule |
| 188 | + .env() |
| 189 | + .getDataClient() |
| 190 | + .mutateRowAsync( |
| 191 | + RowMutation.create(testEnvRule.env().getTableId(), rowKey) |
| 192 | + .addToCell(familyId, "cq", 0, 10) |
| 193 | + .setIdempotency(ByteString.copyFromUtf8("abcdefgh")) |
| 194 | + ) |
| 195 | + .get(1, TimeUnit.MINUTES); |
| 196 | + |
| 197 | + Row row = |
| 198 | + testEnvRule |
| 199 | + .env() |
| 200 | + .getDataClient() |
| 201 | + .readRowsCallable() |
| 202 | + .first() |
| 203 | + .call(Query.create(testEnvRule.env().getTableId()).rowKey(rowKey)); |
| 204 | + |
| 205 | + System.out.println("Shant! " + row.toString()); |
| 206 | + System.out.println("Shant! " + row.getCells().get(0)); |
| 207 | + System.out.println("Shant! " + row.getCells().get(0).getValue().toByteArray()); |
| 208 | + for (byte b : row.getCells().get(0).getValue().toByteArray()) { |
| 209 | + System.out.println("SHant byte " + b); |
| 210 | + } |
| 211 | + System.out.println("Shant bigint " + new BigInteger(row.getCells().get(0).getValue().toByteArray())); |
| 212 | + System.out.println("Shant bytebufsize " + row.getCells().get(0).getValue().size()); |
| 213 | + System.out.println("Shant bytebuf " + java.nio.ByteBuffer.wrap(row.getCells().get(0).getValue().toByteArray()).getInt()); |
| 214 | + System.out.println("Shant bytebuf " + java.nio.ByteBuffer.wrap(row.getCells().get(0).getValue().toByteArray()).order(java.nio.ByteOrder.BIG_ENDIAN).getInt()); |
| 215 | + |
| 216 | + // assertEquals(row.toString(), "shant"); |
| 217 | + assertThat(row.getCells()).hasSize(1); |
| 218 | + // Ensure that the increment is only applied once. |
| 219 | + assertThat(new BigInteger(row.getCells().get(0).getValue().toByteArray())).isEqualTo(10); |
| 220 | + // assertThat(java.nio.ByteBuffer.wrap(row.getCells().get(0).getValue().toByteArray()).order(java.nio.ByteOrder.BIG_ENDIAN).getInt()).isEqualTo(10); |
| 221 | + } |
155 | 222 | } |
0 commit comments