Search in sources :

Example 6 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenSendingStopWithNonExistentIterator_IgnoreRequest.

@Test
public void whenSendingStopWithNonExistentIterator_IgnoreRequest() throws Throwable {
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.receive();
        tx.send(stopRequest(IteratorId.getDefaultInstance()));
        assertEquals(GrpcUtil.doneResponse(), tx.receive().ok());
    }
}
Also used : TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) Test(org.junit.Test)

Example 7 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenOpeningTwoTransactions_TransactionsAreOpenedInDifferentThreads.

@Test
public void whenOpeningTwoTransactions_TransactionsAreOpenedInDifferentThreads() {
    List<Thread> threads = new ArrayList<>();
    when(txFactory.tx(MYKS, GraknTxType.WRITE)).thenAnswer(invocation -> {
        threads.add(Thread.currentThread());
        return tx;
    });
    try (TxGrpcCommunicator tx1 = TxGrpcCommunicator.create(stub);
        TxGrpcCommunicator tx2 = TxGrpcCommunicator.create(stub)) {
        tx1.send(openRequest(MYKS, GraknTxType.WRITE));
        tx2.send(openRequest(MYKS, GraknTxType.WRITE));
    }
    assertNotEquals(threads.get(0), threads.get(1));
}
Also used : TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenExecutingQueryWithInferenceOff_InferenceIsTurnedOff.

@Test
public void whenExecutingQueryWithInferenceOff_InferenceIsTurnedOff() throws InterruptedException {
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.send(execQueryRequest(QUERY, false));
        IteratorId iterator = tx.receive().ok().getIteratorId();
        tx.send(nextRequest(iterator));
        tx.send(stopRequest(iterator));
    }
    verify(tx.graql()).infer(false);
}
Also used : IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) Test(org.junit.Test)

Example 9 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenExecutingAQueryRemotely_AResultIsReturned.

@Test
public void whenExecutingAQueryRemotely_AResultIsReturned() throws InterruptedException {
    Concept conceptX = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptX.getId()).thenReturn(ConceptId.of("V123"));
    when(conceptX.isRelationship()).thenReturn(true);
    when(conceptX.asRelationship().type().getLabel()).thenReturn(Label.of("L123"));
    Concept conceptY = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptY.getId()).thenReturn(ConceptId.of("V456"));
    when(conceptY.isAttribute()).thenReturn(true);
    when(conceptY.asAttribute().type().getLabel()).thenReturn(Label.of("L456"));
    ImmutableList<Answer> answers = ImmutableList.of(new QueryAnswer(ImmutableMap.of(Graql.var("x"), conceptX)), new QueryAnswer(ImmutableMap.of(Graql.var("y"), conceptY)));
    // TODO: reduce wtf
    when(query.results(any())).thenAnswer(params -> query.stream().map(params.<GrpcConverter>getArgument(0)::convert));
    when(query.stream()).thenAnswer(params -> answers.stream());
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.receive();
        tx.send(execQueryRequest(QUERY, null));
        IteratorId iterator = tx.receive().ok().getIteratorId();
        tx.send(nextRequest(iterator));
        TxResponse response1 = tx.receive().ok();
        GrpcConcept.Concept rpcX = GrpcConcept.Concept.newBuilder().setId(V123).setBaseType(BaseType.Relationship).build();
        GrpcGrakn.Answer.Builder answerX = GrpcGrakn.Answer.newBuilder().putAnswer("x", rpcX);
        QueryResult.Builder resultX = QueryResult.newBuilder().setAnswer(answerX);
        assertEquals(TxResponse.newBuilder().setQueryResult(resultX).build(), response1);
        tx.send(nextRequest(iterator));
        TxResponse response2 = tx.receive().ok();
        GrpcConcept.Concept rpcY = GrpcConcept.Concept.newBuilder().setId(V456).setBaseType(BaseType.Attribute).build();
        GrpcGrakn.Answer.Builder answerY = GrpcGrakn.Answer.newBuilder().putAnswer("y", rpcY);
        QueryResult.Builder resultY = QueryResult.newBuilder().setAnswer(answerY);
        assertEquals(TxResponse.newBuilder().setQueryResult(resultY).build(), response2);
        tx.send(nextRequest(iterator));
        TxResponse response3 = tx.receive().ok();
        TxResponse expected = doneResponse();
        assertEquals(expected, response3);
        tx.send(stopRequest(iterator));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 10 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenGettingIsImplicitProperty_IsImplicitIsReturned.

@Test
public void whenGettingIsImplicitProperty_IsImplicitIsReturned() throws InterruptedException {
    ConceptId id = ConceptId.of("V123456");
    Concept concept = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(id)).thenReturn(concept);
    when(concept.isSchemaConcept()).thenReturn(true);
    when(concept.asSchemaConcept().isImplicit()).thenReturn(true);
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.READ));
        tx.receive().ok();
        tx.send(GrpcUtil.runConceptMethodRequest(id, ConceptMethods.IS_IMPLICIT));
        assertTrue(ConceptMethods.IS_IMPLICIT.get(conceptConverter, client, tx.receive().ok()));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Aggregations

TxGrpcCommunicator (ai.grakn.grpc.TxGrpcCommunicator)31 Test (org.junit.Test)31 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)9 Concept (ai.grakn.concept.Concept)8 ConceptId (ai.grakn.concept.ConceptId)8 IteratorId (ai.grakn.rpc.generated.GrpcIterator.IteratorId)7 TxResponse (ai.grakn.rpc.generated.GrpcGrakn.TxResponse)4 GraknException (ai.grakn.exception.GraknException)3 Answer (ai.grakn.graql.admin.Answer)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 Entity (ai.grakn.concept.Entity)1 Label (ai.grakn.concept.Label)1 Role (ai.grakn.concept.Role)1 GrpcGrakn (ai.grakn.rpc.generated.GrpcGrakn)1 Open (ai.grakn.rpc.generated.GrpcGrakn.Open)1 QueryResult (ai.grakn.rpc.generated.GrpcGrakn.QueryResult)1 ArrayList (java.util.ArrayList)1