|
| 1 | +import com.authzed.api.v1.*; |
| 2 | +import com.authzed.grpcutil.BearerToken; |
| 3 | +import com.authzed.grpcutil.RetryableClient; |
| 4 | +import io.grpc.ManagedChannel; |
| 5 | +import io.grpc.ManagedChannelBuilder; |
| 6 | +import io.grpc.Status; |
| 7 | +import io.grpc.StatusRuntimeException; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Random; |
| 15 | + |
| 16 | +import static com.authzed.grpcutil.RetryableClient.ConflictStrategy.*; |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 19 | + |
| 20 | +public class RetryableClientTest { |
| 21 | + |
| 22 | + private ManagedChannel channel; |
| 23 | + private SchemaServiceGrpc.SchemaServiceBlockingStub schemaService; |
| 24 | + private RetryableClient retryableClient; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setUp() { |
| 28 | + channel = ManagedChannelBuilder.forTarget("localhost:50051").usePlaintext().build(); |
| 29 | + // Each test instance gets its own token so tests use isolated SpiceDB namespaces. |
| 30 | + String token = "tc_test_retryable_" + new Random().nextInt(100_000); |
| 31 | + BearerToken bearerToken = new BearerToken(token); |
| 32 | + schemaService = SchemaServiceGrpc.newBlockingStub(channel).withCallCredentials(bearerToken); |
| 33 | + retryableClient = new RetryableClient(channel, bearerToken); |
| 34 | + writeTestSchema(); |
| 35 | + } |
| 36 | + |
| 37 | + @After |
| 38 | + public void tearDown() throws InterruptedException { |
| 39 | + channel.shutdownNow().awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testSuccessfulImport() { |
| 44 | + List<Relationship> rels = relationships("post-1", "alice"); |
| 45 | + // Should complete without throwing. |
| 46 | + retryableClient.retryableBulkImportRelationships(rels, FAIL); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testSkipStrategyIgnoresConflict() { |
| 51 | + List<Relationship> rels = relationships("post-2", "alice"); |
| 52 | + retryableClient.retryableBulkImportRelationships(rels, FAIL); |
| 53 | + // Re-importing the same relationships with SKIP should succeed silently. |
| 54 | + retryableClient.retryableBulkImportRelationships(rels, SKIP); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testTouchStrategyRetriesOnConflict() { |
| 59 | + List<Relationship> rels = relationships("post-3", "alice"); |
| 60 | + retryableClient.retryableBulkImportRelationships(rels, FAIL); |
| 61 | + // Re-importing with TOUCH falls back to WriteRelationships and succeeds. |
| 62 | + retryableClient.retryableBulkImportRelationships(rels, TOUCH); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testFailStrategyThrowsOnConflict() { |
| 67 | + List<Relationship> rels = relationships("post-4", "alice"); |
| 68 | + retryableClient.retryableBulkImportRelationships(rels, FAIL); |
| 69 | + |
| 70 | + assertThatThrownBy(() -> retryableClient.retryableBulkImportRelationships(rels, FAIL)) |
| 71 | + .isInstanceOf(StatusRuntimeException.class) |
| 72 | + .satisfies(e -> assertThat(((StatusRuntimeException) e).getStatus().getCode()) |
| 73 | + .isEqualTo(Status.Code.ALREADY_EXISTS)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testMultipleRelationshipsImport() { |
| 78 | + List<Relationship> rels = Arrays.asList( |
| 79 | + relationship("post-multi", "writer", "alice"), |
| 80 | + relationship("post-multi", "reader", "bob") |
| 81 | + ); |
| 82 | + retryableClient.retryableBulkImportRelationships(rels, FAIL); |
| 83 | + // Both relationships should be importable with TOUCH on re-import. |
| 84 | + retryableClient.retryableBulkImportRelationships(rels, TOUCH); |
| 85 | + } |
| 86 | + |
| 87 | + // --- helpers --- |
| 88 | + |
| 89 | + private List<Relationship> relationships(String postId, String userId) { |
| 90 | + return Arrays.asList(relationship(postId, "writer", userId)); |
| 91 | + } |
| 92 | + |
| 93 | + private Relationship relationship(String postId, String relation, String userId) { |
| 94 | + return Relationship.newBuilder() |
| 95 | + .setResource(ObjectReference.newBuilder() |
| 96 | + .setObjectType("post") |
| 97 | + .setObjectId(postId) |
| 98 | + .build()) |
| 99 | + .setRelation(relation) |
| 100 | + .setSubject(SubjectReference.newBuilder() |
| 101 | + .setObject(ObjectReference.newBuilder() |
| 102 | + .setObjectType("user") |
| 103 | + .setObjectId(userId) |
| 104 | + .build()) |
| 105 | + .build()) |
| 106 | + .build(); |
| 107 | + } |
| 108 | + |
| 109 | + private void writeTestSchema() { |
| 110 | + String schema = "definition post {\n" |
| 111 | + + " relation writer: user\n" |
| 112 | + + " relation reader: user\n" |
| 113 | + + " permission view = reader + writer\n" |
| 114 | + + "}\n" |
| 115 | + + "definition user {}"; |
| 116 | + schemaService.writeSchema(WriteSchemaRequest.newBuilder().setSchema(schema).build()); |
| 117 | + } |
| 118 | +} |
0 commit comments