-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add toByteString/fromByteString for ChangeStreamContinuationTok…
…en (#1346) * feat: Add toByteString/fromByteString for ChangeStreamContinuationToken This will be used by the beam connector to write/read to a Bigtable table. This PR also does: 1. Revert the changes in #1345 since we can use Mockito to create mock objects for testing. * fix: Update comments * fix: Address comments * fix: Add InternalExtensionOnly annotations for Heartbeat/CloseStream/ChangeStreamMutation Co-authored-by: Teng Zhong <[email protected]>
- Loading branch information
1 parent
f1176ae
commit bb5c0c0
Showing
5 changed files
with
164 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...test/java/com/google/cloud/bigtable/data/v2/models/ChangeStreamContinuationTokenTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.cloud.bigtable.data.v2.models; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.bigtable.v2.RowRange; | ||
import com.google.bigtable.v2.StreamContinuationToken; | ||
import com.google.bigtable.v2.StreamPartition; | ||
import com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ChangeStreamContinuationTokenTest { | ||
|
||
private final String TOKEN = "token"; | ||
|
||
private ByteStringRange createFakeByteStringRange() { | ||
return ByteStringRange.create("a", "b"); | ||
} | ||
|
||
// TODO: Get rid of this once we change ChangeStreamContinuationToken::getRowRange() | ||
// to ChangeStreamContinuationToken::getByteStringRange(). | ||
private RowRange rowRangeFromByteStringRange(ByteStringRange byteStringRange) { | ||
return RowRange.newBuilder() | ||
.setStartKeyClosed(byteStringRange.getStart()) | ||
.setEndKeyOpen(byteStringRange.getEnd()) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void basicTest() throws Exception { | ||
ByteStringRange byteStringRange = createFakeByteStringRange(); | ||
ChangeStreamContinuationToken changeStreamContinuationToken = | ||
new ChangeStreamContinuationToken(byteStringRange, TOKEN); | ||
Assert.assertEquals( | ||
changeStreamContinuationToken.getRowRange(), rowRangeFromByteStringRange(byteStringRange)); | ||
Assert.assertEquals(changeStreamContinuationToken.getToken(), TOKEN); | ||
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
ObjectOutputStream oos = new ObjectOutputStream(bos); | ||
oos.writeObject(changeStreamContinuationToken); | ||
oos.close(); | ||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); | ||
ChangeStreamContinuationToken actual = (ChangeStreamContinuationToken) ois.readObject(); | ||
assertThat(actual).isEqualTo(changeStreamContinuationToken); | ||
} | ||
|
||
@Test | ||
public void fromProtoTest() { | ||
ByteStringRange byteStringRange = createFakeByteStringRange(); | ||
RowRange fakeRowRange = rowRangeFromByteStringRange(byteStringRange); | ||
StreamContinuationToken proto = | ||
StreamContinuationToken.newBuilder() | ||
.setPartition(StreamPartition.newBuilder().setRowRange(fakeRowRange).build()) | ||
.setToken(TOKEN) | ||
.build(); | ||
ChangeStreamContinuationToken changeStreamContinuationToken = | ||
ChangeStreamContinuationToken.fromProto(proto); | ||
Assert.assertEquals(changeStreamContinuationToken.getRowRange(), fakeRowRange); | ||
Assert.assertEquals(changeStreamContinuationToken.getToken(), TOKEN); | ||
Assert.assertEquals( | ||
changeStreamContinuationToken, | ||
ChangeStreamContinuationToken.fromProto(changeStreamContinuationToken.toProto())); | ||
} | ||
|
||
@Test | ||
public void toByteStringTest() throws Exception { | ||
ByteStringRange byteStringRange = createFakeByteStringRange(); | ||
ChangeStreamContinuationToken changeStreamContinuationToken = | ||
new ChangeStreamContinuationToken(byteStringRange, TOKEN); | ||
Assert.assertEquals( | ||
changeStreamContinuationToken.getRowRange(), rowRangeFromByteStringRange(byteStringRange)); | ||
Assert.assertEquals(changeStreamContinuationToken.getToken(), TOKEN); | ||
Assert.assertEquals( | ||
changeStreamContinuationToken, | ||
ChangeStreamContinuationToken.fromByteString(changeStreamContinuationToken.toByteString())); | ||
} | ||
} |