/** * Submits a query via the session. * * @param query The query to submit. * @param <T> The query output type. * @return A completable future to be completed with the query output. */ public <T> CompletableFuture<T> submit(Query<T> query) { if (!isOpen()) return Futures.exceptionalFuture(new IllegalStateException("session not open")); CompletableFuture<T> future = new CompletableFuture<>(); context .executor() .execute( () -> { QueryRequest request; if (query.consistency() == Query.ConsistencyLevel.CAUSAL) { request = QueryRequest.builder() .withSession(id) .withSequence(commandResponse) .withVersion(responseVersion) .withQuery(query) .build(); } else { request = QueryRequest.builder() .withSession(id) .withSequence(commandRequest) .withVersion(responseVersion) .withQuery(query) .build(); } submit(request, future); }); return future; }
@Test public void limitMustBePositive() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("limit should be positive: was 0"); QueryRequest.builder().serviceName("foo").limit(0).build(); }
@Test public void endTsMustBePositive() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("endTs should be positive, in epoch microseconds: was 0"); QueryRequest.builder().serviceName("foo").endTs(0L).build(); }
@Test public void binaryAnnotationValueCantBeEmpty() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("binary annotation value for foo was empty"); QueryRequest.builder().serviceName("foo").addBinaryAnnotation("foo", "").build(); }
/** * Particularly in the case of cassandra, indexing boundary annotations isn't fruitful work, and * not helpful to users. Nevertheless we should ensure an unlikely caller gets an exception. */ @Test public void annotationCantBeCore() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("queries cannot be refined by core annotations: sr"); QueryRequest.builder().serviceName("foo").addAnnotation(Constants.SERVER_RECV).build(); }
@Test public void annotationCantBeEmpty() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("annotation was empty"); QueryRequest.builder().serviceName("foo").addAnnotation("").build(); }
@Test public void spanNameCantBeEmpty() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("spanName was empty"); QueryRequest.builder().serviceName("foo").spanName("").build(); }
@Test public void maxDuration_greaterThanOrEqualToMinDuration() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("maxDuration should be >= minDuration"); QueryRequest.builder().serviceName("foo").minDuration(1L).maxDuration(0L).build(); }
@Test public void maxDuration_onlyWithMinDuration() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("maxDuration is only valid with minDuration"); QueryRequest.builder().serviceName("foo").maxDuration(0L).build(); }
@Test public void minDuration_mustBePositive() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("minDuration must be a positive number of microseconds"); QueryRequest.builder().serviceName("foo").minDuration(0L).build(); }
@Test public void annotationQuery_roundTrip() { String annotationQuery = "http.method=GET and error"; QueryRequest request = QueryRequest.builder() .serviceName("security-service") .parseAnnotationQuery(annotationQuery) .build(); assertThat(request.binaryAnnotations).containsEntry(HTTP_METHOD, "GET").hasSize(1); assertThat(request.annotations).containsExactly(Constants.ERROR); assertThat(request.toAnnotationQuery()).isEqualTo(annotationQuery); }
@Test public void annotationQuery_missingValue() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("binary annotation value for http.method was empty"); String annotationQuery = "http.method="; QueryRequest request = QueryRequest.builder() .serviceName("security-service") .parseAnnotationQuery(annotationQuery) .build(); assertThat(request.annotations).containsExactly(HTTP_METHOD); }
@Test public void serviceNameCanBeNull() { assertThat(QueryRequest.builder().build().serviceName).isNull(); }
@Test public void toAnnotationQueryWhenNoInputIsNull() { QueryRequest request = QueryRequest.builder().serviceName("security-service").build(); assertThat(request.toAnnotationQuery()).isNull(); }