-
Notifications
You must be signed in to change notification settings - Fork 62
test: Eliminate repeated if statements in the ReadRows mock service reducing the size of it significantly #1460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
56aa444
0699bf7
f77261a
958a255
5558d00
7589b60
ec545e9
15479c9
588234a
de7d357
228afb1
786fb66
95fd859
e2837a5
c45b0bb
af2955b
68e9d04
a7e13df
5bd8a55
e16b3c9
952a6a9
38b0006
716008a
22848fe
917f3dc
799f5ef
6be2743
8f9ffa0
ab81643
1b0dec8
51e9b85
5418771
ee03787
65644b1
d8092e9
afa3d86
4fdea53
6a6ae48
7431ae1
8389b61
7e2a76c
7162866
882913b
cefe405
8681d45
5ea0cc0
f771841
25ca84f
b907a31
ae3447c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,28 +12,25 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import {ServerWritableStream} from '@grpc/grpc-js'; | ||
| import {protos} from '../../src'; | ||
| import {GoogleError, Status} from 'google-gax'; | ||
| import { | ||
| ChunkGeneratorParameters, | ||
| DebugLog, | ||
| ReadRowsServiceParameters, | ||
| ReadRowsWritableStream, | ||
| } from './readRowsServiceParameters'; | ||
|
|
||
| const DEBUG = process.env.BIGTABLE_TEST_DEBUG === 'true'; | ||
|
|
||
| export function debugLog(text: string) { | ||
| if (DEBUG) { | ||
| console.log(text); | ||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has just been moved to the test and will be passed into the service. |
||
| import {google} from '../../protos/protos'; | ||
| import IRowRange = google.bigtable.v2.IRowRange; | ||
|
|
||
| // Generate documentation for this function | ||
| /** Pretty prints the request object. | ||
| * @param request The request object to pretty print. | ||
| * @param debugLog The logging function for printing test output. | ||
| */ | ||
| export function prettyPrintRequest( | ||
| request: protos.google.bigtable.v2.IReadRowsRequest | ||
| function prettyPrintRequest( | ||
| request: protos.google.bigtable.v2.IReadRowsRequest, | ||
| debugLog: DebugLog | ||
| ) { | ||
| // pretty-printing important parts of the request. | ||
| // doing it field by field because we want to apply .toString() to all key fields | ||
|
|
@@ -79,8 +76,13 @@ export function prettyPrintRequest( | |
| * The fake table contains monotonically increasing zero padded rows | ||
| * in the range [keyFrom, keyTo). | ||
| * @param chunkGeneratorParameters The parameters for generating chunks. | ||
| * @param debugLog The logging function for printing test output. | ||
| * @returns {protos.google.bigtable.v2.ReadRowsResponse.ICellChunk[]} The generated chunks. | ||
| */ | ||
| function generateChunks(chunkGeneratorParameters: ChunkGeneratorParameters) { | ||
| function generateChunks( | ||
| chunkGeneratorParameters: ChunkGeneratorParameters, | ||
| debugLog: DebugLog | ||
| ): protos.google.bigtable.v2.ReadRowsResponse.ICellChunk[] { | ||
| const keyFrom = chunkGeneratorParameters.keyFrom; | ||
| const keyTo = chunkGeneratorParameters.keyTo; | ||
| debugLog(`generating chunks from ${keyFrom} to ${keyTo}`); | ||
|
|
@@ -125,6 +127,11 @@ function generateChunks(chunkGeneratorParameters: ChunkGeneratorParameters) { | |
| return chunks; | ||
| } | ||
|
|
||
| /** Checks if the given key is in the provided RowSet. | ||
| * @param stringKey The key to check. | ||
| * @param rowSet The RowSet to check against. | ||
| * @returns {boolean} True if the key is in the RowSet, false otherwise. | ||
| */ | ||
| function isKeyInRowSet( | ||
| stringKey: string, | ||
| rowSet?: protos.google.bigtable.v2.IRowSet | null | ||
|
|
@@ -163,6 +170,84 @@ function isKeyInRowSet( | |
| return true; | ||
| } | ||
|
|
||
| /** Gets the key value for the given property specified in the request. | ||
| * @param request The request object to get the key value from. | ||
| * @param property The property from the request to get the value from. | ||
| * @returns {string | undefined} The key value from the request. | ||
| */ | ||
| function getKeyValue( | ||
| request: protos.google.bigtable.v2.IReadRowsRequest, | ||
| property: keyof IRowRange | ||
| ) { | ||
| if ( | ||
| request?.rows?.rowRanges && | ||
| request?.rows?.rowRanges[0] && | ||
| request?.rows?.rowRanges[0][property]?.toString() | ||
| ) { | ||
| return request?.rows?.rowRanges[0][property]?.toString(); | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** Gets the key from the request object. | ||
| * @param stream The stream object to get the key from. | ||
| * @param keySelectionParameters The parameters for selecting the key. | ||
| * @returns {number} The selected key for generating chunks | ||
| */ | ||
| function getSelectedKey( | ||
| request: protos.google.bigtable.v2.IReadRowsRequest, | ||
| keySelectionParameters: { | ||
| keyOpenProperty: keyof IRowRange; | ||
| keyClosedProperty: keyof IRowRange; | ||
| defaultKey?: number; | ||
| } | ||
| ) { | ||
| const keyRequestOpen = getKeyValue( | ||
| request, | ||
| keySelectionParameters.keyOpenProperty | ||
| ); | ||
| const keyRequestClosed = getKeyValue( | ||
| request, | ||
| keySelectionParameters.keyClosedProperty | ||
| ); | ||
| const defaultKey = keySelectionParameters.defaultKey; | ||
| return defaultKey !== undefined | ||
| ? defaultKey | ||
| : keyRequestClosed | ||
| ? parseInt(keyRequestClosed as string) | ||
| : parseInt(keyRequestOpen as string) + 1; | ||
| } | ||
|
|
||
| /** Generates chunks for rows in a fake table that match the provided RowSet. | ||
| * The fake table contains monotonically increasing zero padded rows | ||
| * in the range [keyFrom, keyTo). | ||
| * @param request The request object to generate chunks from. | ||
| * @param serviceParameters The parameters for generating chunks. | ||
| * @returns {protos.google.bigtable.v2.ReadRowsResponse.ICellChunk[]} The generated chunks. | ||
| */ | ||
| function generateChunksFromRequest( | ||
| request: protos.google.bigtable.v2.IReadRowsRequest, | ||
| serviceParameters: ReadRowsServiceParameters | ||
| ) { | ||
| return generateChunks( | ||
| { | ||
| keyFrom: getSelectedKey(request, { | ||
| keyOpenProperty: 'startKeyOpen', | ||
| keyClosedProperty: 'startKeyClosed', | ||
| defaultKey: serviceParameters.keyFrom, | ||
| }), | ||
| keyTo: getSelectedKey(request, { | ||
| keyOpenProperty: 'endKeyOpen', | ||
| keyClosedProperty: 'endKeyClosed', | ||
| defaultKey: serviceParameters.keyTo, | ||
| }), | ||
| chunkSize: serviceParameters.chunkSize, | ||
| valueSize: serviceParameters.valueSize, | ||
| }, | ||
| serviceParameters.debugLog | ||
| ); | ||
| } | ||
|
|
||
| // Returns an implementation of the server streaming ReadRows call that would return | ||
| // monotonically increasing zero padded rows in the range [keyFrom, keyTo). | ||
| // The returned implementation can be passed to gRPC server. | ||
|
|
@@ -171,20 +256,11 @@ function isKeyInRowSet( | |
| // TODO: Perhaps group the if statements into classes so that they can be unit tested. | ||
| export function readRowsImpl( | ||
| serviceParameters: ReadRowsServiceParameters | ||
| ): ( | ||
| stream: ServerWritableStream< | ||
| protos.google.bigtable.v2.IReadRowsRequest, | ||
| protos.google.bigtable.v2.IReadRowsResponse | ||
| > | ||
| ) => Promise<void> { | ||
| ): (stream: ReadRowsWritableStream) => Promise<void> { | ||
| let errorAfterChunkNo = serviceParameters.errorAfterChunkNo; | ||
| return async ( | ||
| stream: ServerWritableStream< | ||
| protos.google.bigtable.v2.IReadRowsRequest, | ||
| protos.google.bigtable.v2.IReadRowsResponse | ||
| > | ||
| ): Promise<void> => { | ||
| prettyPrintRequest(stream.request); | ||
| return async (stream: ReadRowsWritableStream): Promise<void> => { | ||
| const debugLog = serviceParameters.debugLog; | ||
| prettyPrintRequest(stream.request, debugLog); | ||
|
|
||
| let stopWaiting: () => void = () => {}; | ||
| let cancelled = false; | ||
|
|
@@ -231,60 +307,7 @@ export function readRowsImpl( | |
| }); | ||
|
|
||
| let chunksSent = 0; | ||
| let keyFromRequestClosed: string | undefined; | ||
| if ( | ||
| stream?.request?.rows?.rowRanges && | ||
| stream?.request?.rows?.rowRanges[0] && | ||
| stream?.request?.rows?.rowRanges[0]?.startKeyClosed?.toString() | ||
| ) { | ||
| keyFromRequestClosed = | ||
| stream?.request?.rows?.rowRanges[0]?.startKeyClosed?.toString(); | ||
| } | ||
| let keyFromRequestOpen: string | undefined; | ||
| if ( | ||
| stream?.request?.rows?.rowRanges && | ||
| stream?.request?.rows?.rowRanges[0] && | ||
| stream?.request?.rows?.rowRanges[0]?.startKeyOpen?.toString() | ||
| ) { | ||
| keyFromRequestOpen = | ||
| stream?.request?.rows?.rowRanges[0]?.startKeyOpen?.toString(); | ||
| } | ||
| let keyToRequestClosed: string | undefined; | ||
| if ( | ||
| stream?.request?.rows?.rowRanges && | ||
| stream?.request?.rows?.rowRanges[0] && | ||
| stream?.request?.rows?.rowRanges[0]?.endKeyClosed?.toString() | ||
| ) { | ||
| keyToRequestClosed = | ||
| stream?.request?.rows?.rowRanges[0]?.endKeyClosed?.toString(); | ||
| } | ||
| let keyToRequestOpen: string | undefined; | ||
| if ( | ||
| stream?.request?.rows?.rowRanges && | ||
| stream?.request?.rows?.rowRanges[0] && | ||
| stream?.request?.rows?.rowRanges[0]?.endKeyOpen?.toString() | ||
| ) { | ||
| keyToRequestOpen = | ||
| stream?.request?.rows?.rowRanges[0]?.endKeyOpen?.toString(); | ||
| } | ||
| const keyFromUsed = | ||
| serviceParameters.keyFrom !== undefined | ||
| ? serviceParameters.keyFrom | ||
| : keyFromRequestClosed | ||
| ? parseInt(keyFromRequestClosed as string) | ||
| : parseInt(keyFromRequestOpen as string) + 1; | ||
| const keyToUsed = | ||
| serviceParameters.keyTo !== undefined | ||
| ? serviceParameters.keyTo | ||
| : keyToRequestClosed | ||
| ? parseInt(keyToRequestClosed as string) | ||
| : parseInt(keyToRequestOpen as string) + 1; | ||
| const chunks = generateChunks({ | ||
| keyFrom: keyFromUsed, | ||
| keyTo: keyToUsed, | ||
| chunkSize: serviceParameters.chunkSize, | ||
| valueSize: serviceParameters.valueSize, | ||
| }); | ||
| const chunks = generateChunksFromRequest(stream.request, serviceParameters); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This single line of code is a substitute for all of the code deleted previous to it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code for each if statement now lives in |
||
| let lastScannedRowKey: string | undefined; | ||
| let currentResponseChunks: protos.google.bigtable.v2.ReadRowsResponse.ICellChunk[] = | ||
| []; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DebugLog is just being moved from the service to here in the test