Skip to content

Commit

Permalink
move MetadataSubject to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf committed Nov 22, 2023
1 parent abad657 commit 9d9a429
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 62 deletions.
2 changes: 0 additions & 2 deletions .kokoro/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ source ${scriptDir}/common.sh
mvn -version
echo ${JOB_TYPE}

mvn dependency:tree

# attempt to install 3 times with exponential backoff (starting with 10 seconds)
retry_with_backoff 3 10 \
mvn install -B -V -ntp \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.google.cloud.bigtable.data.v2.stub;

import static com.google.common.truth.Truth.assertAbout;
import static com.google.cloud.bigtable.data.v2.stub.MetadataSubject.assertThat;
import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.retrying.RetrySettings;
Expand Down Expand Up @@ -48,8 +48,6 @@
import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import io.grpc.ForwardingServerCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
Expand All @@ -66,7 +64,6 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -153,9 +150,9 @@ public void testReadRows() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
assertThat(lastMetadata)
.containsAtLeast(ROUTING_COOKIE_1.name(), "readRows", ROUTING_COOKIE_2.name(), testCookie);
MetadataSubject.assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());
assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());

serverMetadata.clear();
}
Expand All @@ -169,9 +166,9 @@ public void testReadRow() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
assertThat(lastMetadata)
.containsAtLeast(ROUTING_COOKIE_1.name(), "readRows", ROUTING_COOKIE_2.name(), testCookie);
MetadataSubject.assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());
assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());

serverMetadata.clear();
}
Expand All @@ -187,9 +184,10 @@ public void testMutateRows() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
.containsAtLeast(ROUTING_COOKIE_1.name(), "readRows", ROUTING_COOKIE_2.name(), testCookie);
MetadataSubject.assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());
assertThat(lastMetadata)
.containsAtLeast(
ROUTING_COOKIE_1.name(), "mutateRows", ROUTING_COOKIE_2.name(), testCookie);
assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());

serverMetadata.clear();
}
Expand All @@ -203,9 +201,9 @@ public void testMutateRow() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
.containsAtLeast(ROUTING_COOKIE_1.name(), "readRows", ROUTING_COOKIE_2.name(), testCookie);
MetadataSubject.assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());
assertThat(lastMetadata)
.containsAtLeast(ROUTING_COOKIE_1.name(), "mutateRow", ROUTING_COOKIE_2.name(), testCookie);
assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());

serverMetadata.clear();
}
Expand All @@ -220,9 +218,10 @@ public void testSampleRowKeys() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
.containsAtLeast(ROUTING_COOKIE_1.name(), "readRows", ROUTING_COOKIE_2.name(), testCookie);
MetadataSubject.assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());
assertThat(lastMetadata)
.containsAtLeast(
ROUTING_COOKIE_1.name(), "sampleRowKeys", ROUTING_COOKIE_2.name(), testCookie);
assertThat(lastMetadata).doesNotContainKeys(BAD_KEY.name());

serverMetadata.clear();
}
Expand All @@ -238,8 +237,7 @@ public void testNoCookieSucceedReadRows() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
.doesNotContainKeys(ROUTING_COOKIE_1.name(), ROUTING_COOKIE_2.name());
assertThat(lastMetadata).doesNotContainKeys(ROUTING_COOKIE_1.name(), ROUTING_COOKIE_2.name());

serverMetadata.clear();
}
Expand All @@ -255,7 +253,7 @@ public void testNoCookieSucceedReadRow() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
assertThat(lastMetadata)
.doesNotContainKeys(ROUTING_COOKIE_1.name(), ROUTING_COOKIE_2.name(), BAD_KEY.name());

serverMetadata.clear();
Expand All @@ -274,7 +272,7 @@ public void testNoCookieSucceedMutateRows() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
assertThat(lastMetadata)
.doesNotContainKeys(ROUTING_COOKIE_1.name(), ROUTING_COOKIE_2.name(), BAD_KEY.name());

serverMetadata.clear();
Expand All @@ -291,7 +289,7 @@ public void testNoCookieSucceedMutateRow() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
assertThat(lastMetadata)
.doesNotContainKeys(ROUTING_COOKIE_1.name(), ROUTING_COOKIE_2.name(), BAD_KEY.name());

serverMetadata.clear();
Expand All @@ -308,7 +306,7 @@ public void testNoCookieSucceedSampleRowKeys() {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
assertThat(lastMetadata)
.doesNotContainKeys(ROUTING_COOKIE_1.name(), ROUTING_COOKIE_2.name(), BAD_KEY.name());

serverMetadata.clear();
Expand Down Expand Up @@ -357,7 +355,7 @@ public void sendHeaders(Metadata headers) {

Metadata lastMetadata = serverMetadata.get(fakeService.count.get() - 1);

MetadataSubject.assertThat(lastMetadata)
assertThat(lastMetadata)
.containsAtLeast(
ROUTING_COOKIE_2.name(), testCookie, routingCookieKey.name(), routingCookieValue);

Expand Down Expand Up @@ -550,40 +548,4 @@ private void maybePopulateCookie(Metadata trailers, String label) {
}
}
}

static final class MetadataSubject extends Subject {

@Nullable private final Metadata actual;

public static Factory<MetadataSubject, Metadata> metadata() {
return MetadataSubject::new;
}

private MetadataSubject(FailureMetadata metadata, @Nullable Metadata actual) {
super(metadata, actual);
this.actual = actual;
}

public static MetadataSubject assertThat(@Nullable Metadata actual) {
return assertAbout(metadata()).that(actual);
}

public void containsAtLeast(String... keyValuePairs) {
assert actual != null;
for (int i = 0; i < keyValuePairs.length; i += 2) {
check("containsAtLeast()")
.that(actual.get(Metadata.Key.of(keyValuePairs[i], Metadata.ASCII_STRING_MARSHALLER)))
.isEqualTo(keyValuePairs[i + 1]);
}
}

public void doesNotContainKeys(String... keys) {
assert actual != null;
for (String key : keys) {
check("doesNotContainKeys()")
.that(actual.containsKey(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)))
.isFalse();
}
}
}
}

0 comments on commit 9d9a429

Please sign in to comment.