Skip to content
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

ServiceTestStub methods not implemented #339

Closed
cepages opened this issue Dec 4, 2018 · 10 comments · Fixed by #378
Closed

ServiceTestStub methods not implemented #339

cepages opened this issue Dec 4, 2018 · 10 comments · Fixed by #378

Comments

@cepages
Copy link

cepages commented Dec 4, 2018

I'm trying to test my grpc layer using ...ServiceTestStub.

If the rpc has a stream I can create all needed to test it. Also If the rpc doesn't have a stream but only if I use the synchronous method.

If I implement the asynchronous call I get an exception fatalError("not implemented")

In the code generated for the stubs seems that there is not implementation for this type of method:

  func list_protocols(_ request: Xz_Rpc_Protocol_ListProtocolsRequest, completion: @escaping (Xz_Rpc_Protocol_ListProtocolsResponse?, CallResult) -> Void) throws -> Xz_Rpc_Protocol_ProtocolServicelist_protocolsCall {
    fatalError("not implemented")
  }

Do I have to create the stub manually to test this type of rpc's?

@MrMage
Copy link
Collaborator

MrMage commented Dec 4, 2018 via email

@cepages
Copy link
Author

cepages commented Dec 4, 2018

Yeah I could try.

Because the code is generated I guess is not that simple to implement it in the .grpc file.

Could you point me where I can create the implementation so it's generated when the script is run?

@MrMage
Copy link
Collaborator

MrMage commented Dec 4, 2018 via email

@cepages
Copy link
Author

cepages commented Dec 4, 2018

@MrMage trying to follow your advise of implementing the synchronous call and call back to the completion closure I came across two problems: the CallResult and the object to return the ...Call object, where can I get those?

@cepages
Copy link
Author

cepages commented Dec 6, 2018

@MrMage after diving a bit more in the code, I could mock the ServiceTestStub method in my test system, I guess I could do something similar in the generated code:

class Xz_Rpc_Protocol_ProtocolServiceServiceTestStubFixed: Xz_Rpc_Protocol_ProtocolServiceServiceTestStub {
    
    override func list_protocols(_ request: Xz_Rpc_Protocol_ListProtocolsRequest, completion: @escaping (Xz_Rpc_Protocol_ListProtocolsResponse?, CallResult) -> Void) throws -> Xz_Rpc_Protocol_ProtocolServicelist_protocolsCall {
        
        let response = try self.list_protocols(request)
        
        let channel = Channel(address: "")
        let call = channel.makeCall("")
        let operationGroup = try OperationGroup(call: call, operations: [])
        let callresult = CallResult(operationGroup)

        completion(response,callresult)
        
        return Servicelist_protocolsCall()
    }
}

The line Servicelist_protocolsCall() creates an object from a class that conforms the protocolXz_Rpc_Protocol_ProtocolServicelist_protocolsCall

But I'm pretty sure I could use in the Xz_Rpc_Protocol_ProtocolServicelist_protocolsCallBase(channel) in the generator instead, because that class is implemented as a fileprivate in the code generated

If you are happy, I could try to implement this in the generator.

If not, I hope this doesn't seem like a totally atrocity :S

@MrMage
Copy link
Collaborator

MrMage commented Dec 10, 2018

Ouch! Sorry, I had forgotten that CallResult was so cumbersome to initialize. I would suggest making this method public:

https://github.com/grpc/grpc-swift/blob/c401b44ea81b246b8e7fea191ea1ee11a834ee60/Sources/SwiftGRPC/Core/CallResult.swift#L44

And then implement the method as follows:

    override func list_protocols(_ request: Xz_Rpc_Protocol_ListProtocolsRequest, completion: @escaping (Xz_Rpc_Protocol_ListProtocolsResponse?, CallResult) -> Void) throws -> Xz_Rpc_Protocol_ProtocolServicelist_protocolsCall {
        let response = try self.list_protocols(request)
        let callResult = CallResult(success: true, statusCode: .ok, statusMessage: "OK", resultData: nil,
                   initialMetadata: nil, trailingMetadata: nil)
        completion(response, callResult)
        return Servicelist_protocolsCall()
    }

Hope that makes sense; let me know if you have further questions.

@cepages
Copy link
Author

cepages commented Feb 17, 2019

@MrMage I totally forgot to finish this, sorry.

I've made the changes and created a test in ClientTestExample.

But I can't see any testing target where I can run the test. ( Trying with SwiftGRPC-Carthage.xcodeproj )

Also I'm trying with make test-echo but I get an error with #include <nghttp2/nghttp2.h>

@cepages
Copy link
Author

cepages commented Feb 17, 2019

Ok, I think cleaning the project helped.

I can get some input with make test-echo

@MrMage
Copy link
Collaborator

MrMage commented Feb 18, 2019

You should run make project, then use the resulting project to run the tests. If you file a pull request, the tests will also be run on our Continuous Integration server and I can review them. To fix the dependency issue, you can run brew install nghttp2 if you have homebrew installed. But if you only run the SwiftGRPCTests test target, the issue should not even come up.

@cepages
Copy link
Author

cepages commented Feb 24, 2019

At the end I needed to install nghhtp2 to run any test. They all pass nicely.
Thanks for your help

@cepages cepages closed this as completed Feb 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants