YoshiyukiKato / grpc-mock

A simple mock gRPC server on Node.js
MIT License
85 stars 24 forks source link

Simple error response support implemented #6

Closed aborovsky closed 5 years ago

aborovsky commented 5 years ago

Current PR can't be approved right now, cause there are no test coverage for new feature.

@YoshiyukiKato i need your help. I started to implement tests. But before any modification, i've tried to run current tests and they failed. What i'm doing wrong?

Tests run log:

$ $(npm bin)/mocha --require intelli-espower-loader test

grpc-mock
✓ responds Hello
✓ responds Goodbye
1) records the interactions
2) records the interactions when there are no valid responses
client stream
✓ responds how are you
✓ records the interactions when there are valid responses
3) records the interactions when there are no valid responses
server stream
4) responds nice to meet you
mutual stream
5) responds chat

4 passing (119ms)
5 failing

1) grpc-mock
records the interactions:
Error: 2 UNKNOWN: Stream removed
at Object.exports.createStatusError (node_modules/grpc/src/common.js:91:15)
at Object.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:1204:28)
at InterceptingListener._callNext (node_modules/grpc/src/client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:618:8)
at callback (node_modules/grpc/src/client_interceptors.js:845:24)

2) grpc-mock
records the interactions when there are no valid responses:

Uncaught AssertionError [ERR_ASSERTION]:   # test/index.test.js:79

assert.deepEqual(mockServer.getInteractionsOn("hello"), [{ message: "test1" }])
|          |                           ||
|          |                           |Object{message:"test1"}
|          []                          [#Object#]
GrpcServer{server:#Server#,getInteractionsOn:#function#,clearInteractions:#function#}

+ expected - actual

-[]
+[
+  {
+    "message": "test1"
+  }
+]

at Decorator._callFunc (node_modules/empower-core/lib/decorator.js:110:20)
at Decorator.concreteAssert (node_modules/empower-core/lib/decorator.js:103:17)
at Function.decoratedAssert [as deepEqual] (node_modules/empower-core/lib/decorate.js:49:30)
at Timeout.setTimeout (test/index.test.js:79:14)

3) grpc-mock
client stream
records the interactions when there are no valid responses:

Uncaught AssertionError [ERR_ASSERTION]:   # test/index.test.js:124

assert.deepEqual(mockServer.getInteractionsOn("howAreYou"), [{ message: "Hi" },{ message: "Unexpected message" },{ message: "How are you?" }])
|          |                               ||                 |                                 |
|          |                               ||                 |                                 Object{message:"How are you?"}
|          |                               ||                 Object{message:"Unexpected message"}
|          |                               |Object{message:"Hi"}
|          []                              [#Object#,#Object#,#Object#]
GrpcServer{server:#Server#,getInteractionsOn:#function#,clearInteractions:#function#}

+ expected - actual

-[]
+[
+  {
+    "message": "Hi"
+  }
+  {
+    "message": "Unexpected message"
+  }
+  {
+    "message": "How are you?"
+  }
+]

at Decorator._callFunc (node_modules/empower-core/lib/decorator.js:110:20)
at Decorator.concreteAssert (node_modules/empower-core/lib/decorator.js:103:17)
at Function.decoratedAssert [as deepEqual] (node_modules/empower-core/lib/decorate.js:49:30)
at Timeout.setTimeout (test/index.test.js:124:18)

4) grpc-mock
server stream
responds nice to meet you:
Uncaught Error: 2 UNKNOWN: Stream removed
at Object.exports.createStatusError (node_modules/grpc/src/common.js:91:15)
at ClientReadableStream._emitStatusIfDone (node_modules/grpc/src/client.js:233:26)
at ClientReadableStream._receiveStatus (node_modules/grpc/src/client.js:211:8)
at Object.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:1272:15)
at InterceptingListener._callNext (node_modules/grpc/src/client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:618:8)
at ~/work_dir/grpc-mock/node_modules/grpc/src/client_interceptors.js:1029:24

5) grpc-mock
mutual stream
responds chat:
Uncaught Error: 2 UNKNOWN: Stream removed
at Object.exports.createStatusError (node_modules/grpc/src/common.js:91:15)
at ClientDuplexStream._emitStatusIfDone (node_modules/grpc/src/client.js:233:26)
at ClientDuplexStream._receiveStatus (node_modules/grpc/src/client.js:211:8)
at Object.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:1306:15)
at InterceptingListener._callNext (node_modules/grpc/src/client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:618:8)
at ~/work_dir/grpc-mock/node_modules/grpc/src/client_interceptors.js:1123:18
YoshiyukiKato commented 5 years ago

@aborovsky Hi! Thank you for your PR. I checked out this PR to my local and run test, but there are no error occurred...🤔 Do you have any other changes in your local? Or, It could be an environment problem. Could you please tell me your node and npm versions? (FYI, my node version is v11.6.0, and npm version is v6.7.0)

aborovsky commented 5 years ago

@YoshiyukiKato no changes compared to my forked master branch:

➜  grpc-mock git:(master) ✗ git remote -v   
origin  https://github.com/aborovsky/grpc-mock (fetch)
origin  https://github.com/aborovsky/grpc-mock (push)
➜  grpc-mock git:(master) ✗ git status -sb
## master...origin/master
?? .idea/

My environment:

➜  grpc-mock git:(master) ✗ node --version    
v11.7.0
➜  grpc-mock git:(master) ✗ npm --version 
6.7.0
➜  grpc-mock git:(master) ✗ python --version 
Python 2.7.10
➜  grpc-mock git:(master) ✗ git --version             
git version 2.17.2 (Apple Git-113)
➜  grpc-mock git:(master) ✗ uname -a
Darwin MacBook-Pro-boba.local 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64
YoshiyukiKato commented 5 years ago

@aborovsky

Hmm...I cannot reproduce the error.

It might be a port problem. Please try to change the port of test server and client?

aborovsky commented 5 years ago

Ok, I’ll try

пт, 8 февр. 2019 г. в 20:35, Yoshiyuki Kato notifications@github.com:

@aborovsky https://github.com/aborovsky

Hmm...I cannot reproduce the error.

It might be a port problem. Please try to change the port of test server and client?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/YoshiyukiKato/grpc-mock/pull/6#issuecomment-461883082, or mute the thread https://github.com/notifications/unsubscribe-auth/ABl5LEYbpSWruuqesJx6WFQisB9FMGD5ks5vLbVGgaJpZM4audMR .

-- Отправлено с iPad.

aborovsky commented 5 years ago

@YoshiyukiKato tests adeed. We are good to go with review and merge current PR ;)

aborovsky commented 5 years ago

@YoshiyukiKato thanks a lot for great PR review! I've resolved all issues.

aborovsky commented 5 years ago

I think it is better not to use Error constructor, because it occurs difference of return type of the method between when _error has metadata and when it does not have.

GRPC’s frror is instance of Error. As far as i know. So it’s better to be compliant.

ср, 13 марта 2019 г. в 21:08, Yoshiyuki Kato notifications@github.com:

@YoshiyukiKato commented on this pull request.

@aborovsky https://github.com/aborovsky Thank you for updating! I wrote some comments about coding styles. Please check them out ;)

In index.js https://github.com/YoshiyukiKato/grpc-mock/pull/6#discussion_r265234529:

const m = new Metadata();

  • Object.keys(error.metadata).forEach(key => m.add(key, String(metadata[key])));
  • Object.keys(metadata).forEach(key => m.add(key, String(metadata[key])));
  • [
  • ...Object.keys(_error),
  • "name", "message", "stack"

according to doc, grpc service error https://grpc.io/grpc/node/grpc.html#~ServiceError does not have name, message, and stack. Could you tell me why add these properties?

In index.js https://github.com/YoshiyukiKato/grpc-mock/pull/6#discussion_r265254830:

}

  • const { metadata } = error;
  • const error = new Error();

I think it is better not to use Error constructor, because it occurs difference of return type of the method between when _error has metadata and when it does not have.

In index.js https://github.com/YoshiyukiKato/grpc-mock/pull/6#discussion_r265259741:

const m = new Metadata();

  • Object.keys(error.metadata).forEach(key => m.add(key, String(metadata[key])));
  • Object.keys(metadata).forEach(key => m.add(key, String(metadata[key])));
  • [
  • ...Object.keys(_error),
  • "name", "message", "stack"
  • ]
  • .map(fieldName => ({
  • key: fieldName,
  • value: _error[fieldName]
  • }))
  • .filter(({ key }) => (key !== "metadata" && _error.hasOwnProperty(key)))
  • .forEach(({ key, value }) => error[key] = value);

I think prepareMetadata method is correct but redundant a little. It could be written more simply by using reduce method as follows:

const prepareMetadata = error => { if (!error.metadata) { return _error; }

const { metadata, code } = error; const grpcMetadata = Object.values(metadata).reduce((m, [k, v] => m.add(k, String(v)), new Metadata());

return { code, metadata: grpcMetadata } }

if you want to allow extra parameters, object spread syntax will help you.

const prepareMetadata = error => { if (!error.metadata) { return _error; }

const { metadata, code, ..._error } = error; const grpcMetadata = Object.values(metadata).reduce((m, [k, v] => m.add(k, String(v)), new Metadata());

return { code, metadata: grpcMetadata, ..._error } }

what do you think about these?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/YoshiyukiKato/grpc-mock/pull/6#pullrequestreview-214089633, or mute the thread https://github.com/notifications/unsubscribe-auth/ABl5LL-gMU4OJGLXJnJ1p8zBDF1Ck8eYks5vWT6sgaJpZM4audMR .

-- Отправлено с iPad.

aborovsky commented 5 years ago

@YoshiyukiKato please, review latest version

YoshiyukiKato commented 5 years ago

@aborovsky Thank you for updating. It looks great! I'm very happy to merge this PR. Thank you so much😆