Skip to content

test: begin to refactor the mock service#1447

Merged
danieljbruce merged 21 commits into3527322442-to-main-2from
3527322442-refactor-the-test-into-classes
Jul 23, 2024
Merged

test: begin to refactor the mock service#1447
danieljbruce merged 21 commits into3527322442-to-main-2from
3527322442-refactor-the-test-into-classes

Conversation

@danieljbruce
Copy link
Contributor

@danieljbruce danieljbruce commented Jul 16, 2024

Summary:

The ultimate goal is to address the TODOs mentioned in

// TODO: Remove optional keyFrom, keyTo from the server. No test uses them. Remove them from this test as well.
// TODO: Address the excessive number of if statements.
// TODO: Perhaps group the if statements into classes so that they can be unit tested.
. That is, we want to make the ReadRows mock service more modular and first there should ideally just be one copy of it. This PR contains initial steps towards that goal with the intent of merging more PRs in the future to arrive at a more modular mock service that is unit tested.

We are going to stack PRs onto this branch and then make one final merge into main.

Plan:

Currently there are 2 ReadRows mock services with a bunch of code that is duplicated. Before we address the excessive amount of if statements we need to take steps to eliminate similar services ReadRowsImpl1 and ReadRowsImpl2. And before we eliminate duplicate code in ReadRowsImpl and ReadRowsImpl2 we should take some basic steps toward trying to eliminate ReadRowsImpl2 entirely by allowing for additional configurations to be made to ReadRowsImpl so that it can be reused.

In this PR:

  1. Change the existing mock service so that it accepts parameters for chunk size, value size and chunks per response and pass these parameters in each time a service is defined. The two services ReadRowsImpl, ReadRowsImpl2 each rely on a different set of constants for chunk size, value size and chunks per response. Allowing them to be configurable in ReadRowsImpl generalizes ReadRowsImpl and is one step toward our plan of replacing ReadRowsImpl2 with a generalized version of ReadRowsImpl.
  2. To achieve 1 without having functions with 3+ parameters which is bad engineering practice we need to create new interfaces ReadRowsServiceParameters and ChunkGeneratorParameters so that readRowsImpl and generateChunks only need one parameter passed in.
  3. In the tests we define const keyFrom = 0; and const keyTo = 1000; many times and we can just rely on a constant for these defined at the top of the file.
  4. We begin to trim code from ReadRowsImpl2 by removing prettyPrintRequest because it is an exact duplicate of the prettyPrintRequest function in ReadRowsImpl.

Next steps:

  1. Eliminate more duplicate code in ReadRowsImpl2.
  2. Try to replace all usages of ReadRowsImpl2 with a generalized version of ReadRowsImpl.
  3. Address issue with too many if statements in ReadRowsImpl. Introduce classes, generate unit tests, make the service more modular.

@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: bigtable Issues related to the googleapis/nodejs-bigtable API. labels Jul 16, 2024
@danieljbruce danieljbruce marked this pull request as ready for review July 17, 2024 13:59
@danieljbruce danieljbruce requested a review from a team as a code owner July 17, 2024 13:59
@danieljbruce danieljbruce requested a review from a team July 17, 2024 13:59
Copy link
Contributor

@leahecole leahecole left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nits mostly. Would love @sofisl to take a glance too if feasible

test/readrows.ts Outdated

service.setService({
ReadRows: readRowsImpl(keyFrom, keyTo) as any,
ReadRows: readRowsImpl(STANDARD_SERVICE_WITHOUT_ERRORS) as any,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love it if we could have no typecasting to any in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just replaced all these instances with as ServerImplementationInterface.

test/readrows.ts Outdated
valueSize: VALUE_SIZE,
chunkSize: CHUNK_SIZE,
chunksPerResponse: CHUNKS_PER_RESPONSE,
errorAfterChunkNo: 423,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why we're using 423 or is it just an arbitrary spot to error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just the value that Alex used for this test. It looks like it is just a value that is as random as possible to make the test realistic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if we could use Math.random() to test it more realistically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an additional test to emit an error at a random position. I want to keep the old test in place too so that if it breaks then we have predictable code we can use to debug the issue.

import {ServerWritableStream} from '@grpc/grpc-js';
import {protos} from '../../src';
import {GoogleError, Status} from 'google-gax';
import {prettyPrintRequest} from './readRowsImpl';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(feel free to resolve) YAY FOR REUSING THINGS 😁

danieljbruce and others added 2 commits July 17, 2024 10:58
Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
import {ReadRowsServiceParameters} from '../test/utils/readRowsServiceParameters';

// Define parameters for a standard Bigtable Mock service
const VALUE_SIZE = 1024 * 1024;
Copy link
Contributor

@sofisl sofisl Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does value size represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment for this. It is // An upper bound on the amount of data included in the chunks

@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Jul 17, 2024
@danieljbruce danieljbruce requested review from leahecole and sofisl July 22, 2024 15:43
@danieljbruce danieljbruce changed the base branch from 3527322442-to-main to 3527322442-to-main-2 July 22, 2024 15:52
@danieljbruce danieljbruce reopened this Jul 22, 2024
@danieljbruce danieljbruce merged commit 6c1df10 into 3527322442-to-main-2 Jul 23, 2024
@danieljbruce danieljbruce deleted the 3527322442-refactor-the-test-into-classes branch July 23, 2024 20:18
danieljbruce added a commit that referenced this pull request Sep 23, 2024
* test: begin to refactor the mock service (#1447)

* Use prettyPrintRequest from readRowsImpl1

* Define interfaces for creating a Bigtable service

* Remove keyFrom and keyTo from each test

* Define the service inline for standard keys errors

* Add a comment about the refactor steps I am doing

* Add a header to the service parameters file

* Use the ChunkGeneratorParameters interface

* Simplify the diff by creating local variables

* Remove comment

* Eliminate chunks per response constant

* Change imports

* Replace with as ServerImplementationInterface

* Update test/readrows.ts

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* Add a second test for an error at a random pos

* Add some comments for documentation

* Chunk generation - add the parameter

* Added comments to all the interfaces

* Fix a regression bug from the merge

---------

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* test: Eliminate the second ReadRows Service and use a generalized version of the first ReadRows (#1457)

* Use prettyPrintRequest from readRowsImpl1

* Define interfaces for creating a Bigtable service

* Remove keyFrom and keyTo from each test

* Define the service inline for standard keys errors

* Add a comment about the refactor steps I am doing

* Add a header to the service parameters file

* Use the ChunkGeneratorParameters interface

* Simplify the diff by creating local variables

* Remove comment

* Eliminate chunks per response constant

* Change imports

* Set up the second ReadRows service to use params

* Remove duplicate copy of generate chunks from serv

* Remove second copy of isKeyInRowSet

* Eliminate duplicate copies of the debug log

* Fix a bug for the to and from service parameters

Only ignore keyFrom and keyTo when they are undefined and not just when they are Falsy.

* Add cancel back into the mock service for testing

* Add variables to match service 1

* Add one more check to match the other service

* Remove usages of ReadRows2Impl

* Remove the new service

The old service has been generalized enough to mock correct server behaviour.

* Moved the position of the comment for 150 rows

* Eliminate setting keyTo and keyFrom

they are undefined anyway.

* Add a second test for an error at a random pos

* Add some comments for documentation

* Chunk generation - add the parameter

* Added comments to all the interfaces

* Delete file

* Change splitted to split

* Remove export

* Don’t rename the interfaces

There is no point

* Increase Windows timeout

* Provide functions to eliminate if statements

* Eliminate the if statements

Make a direct call to generateChunksFromRequest

* Revert "Eliminate the if statements"

This reverts commit 0996e89.

* Revert "Provide functions to eliminate if statements"

This reverts commit 4a4761f.

* Change any’s to string | undefined

* Eliminate duplicate code for setting timeouts

* remove only

* Update test/readrows.ts

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* Update test/readrows.ts

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* Update test/readrows.ts

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* Update test/readrows.ts

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* Update test/readrows.ts

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* Update test/readrows.ts

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* Update test/readrows.ts

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

---------

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>

* test: Eliminate repeated if statements in the ReadRows mock service reducing the size of it significantly (#1460)

* Use prettyPrintRequest from readRowsImpl1

* Define interfaces for creating a Bigtable service

* Remove keyFrom and keyTo from each test

* Define the service inline for standard keys errors

* Add a comment about the refactor steps I am doing

* Add a header to the service parameters file

* Use the ChunkGeneratorParameters interface

* Simplify the diff by creating local variables

* Remove comment

* Eliminate chunks per response constant

* Change imports

* Set up the second ReadRows service to use params

* Remove duplicate copy of generate chunks from serv

* Remove second copy of isKeyInRowSet

* Eliminate duplicate copies of the debug log

* Fix a bug for the to and from service parameters

Only ignore keyFrom and keyTo when they are undefined and not just when they are Falsy.

* Add cancel back into the mock service for testing

* Add variables to match service 1

* Add one more check to match the other service

* Remove usages of ReadRows2Impl

* Remove the new service

The old service has been generalized enough to mock correct server behaviour.

* Moved the position of the comment for 150 rows

* Eliminate setting keyTo and keyFrom

they are undefined anyway.

* Add a second test for an error at a random pos

* Add some comments for documentation

* Chunk generation - add the parameter

* Added comments to all the interfaces

* Group the property getter into a function

* Group key selection into functions

* Fix typo: default key

* Documentation for isInRowSet

* Eliminate variable - move function inline

* Omit optional selector on stream

* Create ReadRowsWritableStream interface

* Use new interface, remove ServerWritableStream

* Don’t pass the whole stream into helpers

* Add a function for generating the chunks

* The debug log should be a pass through parameter

* Solve compiler errors resulting immediately from

merge

* Add debugLog parameter to various functions

* Add return type

* Remove exports - functions are only used in this

module

* Revise merge correction

* Remove TODO for the stream type

* Update the getKeyValue function

* Eliminate place where debug log is needed

* Run linter

* test: Break the ReadRows service down into classes instead of a single long function (#1461)

* Use prettyPrintRequest from readRowsImpl1

* Define interfaces for creating a Bigtable service

* Remove keyFrom and keyTo from each test

* Define the service inline for standard keys errors

* Add a comment about the refactor steps I am doing

* Add a header to the service parameters file

* Use the ChunkGeneratorParameters interface

* Simplify the diff by creating local variables

* Remove comment

* Eliminate chunks per response constant

* Change imports

* Set up the second ReadRows service to use params

* Remove duplicate copy of generate chunks from serv

* Remove second copy of isKeyInRowSet

* Eliminate duplicate copies of the debug log

* Fix a bug for the to and from service parameters

Only ignore keyFrom and keyTo when they are undefined and not just when they are Falsy.

* Add cancel back into the mock service for testing

* Add variables to match service 1

* Add one more check to match the other service

* Remove usages of ReadRows2Impl

* Remove the new service

The old service has been generalized enough to mock correct server behaviour.

* Moved the position of the comment for 150 rows

* Eliminate setting keyTo and keyFrom

they are undefined anyway.

* Add a second test for an error at a random pos

* Add some comments for documentation

* Chunk generation - add the parameter

* Added comments to all the interfaces

* Group the property getter into a function

* Group key selection into functions

* Fix typo: default key

* Documentation for isInRowSet

* Eliminate variable - move function inline

* Omit optional selector on stream

* Create ReadRowsWritableStream interface

* Use new interface, remove ServerWritableStream

* Don’t pass the whole stream into helpers

* Add a function for generating the chunks

* The debug log should be a pass through parameter

* Add some TODOs about how to address this next

* Pull send response into a class

* Create a dedicated class for defining the service

* Change name to readRowsRequestHandler

* Pull the function that generates the chunks into

Separate module

* Generate documentation

* Add more documentation to the Service class

* Add debugLog as a method parameter

* Solve compiler errors resulting immediately from

merge

* Add debugLog parameter to various functions

* Add return type

* Remove exports - functions are only used in this

module

* Revise merge correction

* Remove TODO for the stream type

* Update the getKeyValue function

* Eliminate place where debug log is needed

* Run linter

* Eliminate the function that creates a service

Add a factory method instead

* Add documentation for the constructor

* Remove the TODO

* Set the timeout for the test

* Increase the timeout

* Eliminate ternary statement

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Add a couple of comments

* re-write the ternary expression

---------

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigtable Issues related to the googleapis/nodejs-bigtable API. size: l Pull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants