Esempio n. 1
0
    public async Task RPCMethodNameSubstitutionByOptions()
    {
        var streams = FullDuplexStream.CreateStreams();

        this.serverStream = streams.Item1;
        this.clientStream = streams.Item2;

        var camelCaseOptions = new JsonRpcProxyOptions {
            MethodNameTransform = CommonMethodNameTransforms.CamelCase
        };
        var prefixOptions = new JsonRpcProxyOptions {
            MethodNameTransform = CommonMethodNameTransforms.Prepend("ns.")
        };

        // Construct two client proxies with conflicting method transforms to prove that each instance returned retains its unique options.
        var clientRpc = new JsonRpc(this.clientStream, this.clientStream);
        var clientRpcWithCamelCase = clientRpc.Attach <IServer3>(camelCaseOptions);
        var clientRpcWithPrefix    = clientRpc.Attach <IServer3>(prefixOptions);

        clientRpc.StartListening();

        // Construct the server to only respond to one set of method names for now to confirm that the client is sending the right one.
        this.serverRpc = new JsonRpc(this.serverStream, this.serverStream);
        this.serverRpc.AddLocalRpcTarget(this.server, new JsonRpcTargetOptions {
            MethodNameTransform = camelCaseOptions.MethodNameTransform
        });
        this.serverRpc.StartListening();

        Assert.Equal("Hi!", await clientRpcWithCamelCase.SayHiAsync());                                             // "sayHiAsync"
        await Assert.ThrowsAsync <RemoteMethodNotFoundException>(() => clientRpcWithPrefix.SayHiAsync());           // "ns.SayHiAsync"

#if !NETCOREAPP1_0                                                                                                  // skip attribute-based renames where not supported
        Assert.Equal("ANDREW", await clientRpcWithCamelCase.ARoseByAsync("andrew"));                                // "anotherName"
        await Assert.ThrowsAsync <RemoteMethodNotFoundException>(() => clientRpcWithPrefix.ARoseByAsync("andrew")); // "ns.AnotherName"
#endif

        // Prepare the server to *ALSO* accept method names with a prefix.
        this.serverRpc.AllowModificationWhileListening = true;
        this.serverRpc.AddLocalRpcTarget(this.server, new JsonRpcTargetOptions {
            MethodNameTransform = prefixOptions.MethodNameTransform
        });

        // Retry with our second client proxy to send messages which the server should now accept.
        Assert.Equal("Hi!", await clientRpcWithPrefix.SayHiAsync());              // "ns.SayHiAsync"
#if !NETCOREAPP1_0                                                                // skip attribute-based renames where not supported
        Assert.Equal("ANDREW", await clientRpcWithPrefix.ARoseByAsync("andrew")); // "ns.AnotherName"
#endif
    }
Esempio n. 2
0
    public async Task NamingTransformsAreAppliedToEvents()
    {
        var streams = FullDuplexStream.CreateStreams();

        this.serverStream = streams.Item1;
        this.clientStream = streams.Item2;

        var camelCaseOptions = new JsonRpcProxyOptions {
            EventNameTransform = CommonMethodNameTransforms.CamelCase
        };
        var prefixOptions = new JsonRpcProxyOptions {
            EventNameTransform = CommonMethodNameTransforms.Prepend("ns.")
        };

        // Construct two client proxies with conflicting method transforms to prove that each instance returned retains its unique options.
        var clientRpc = new JsonRpc(this.clientStream, this.clientStream);
        var clientRpcWithCamelCase = clientRpc.Attach <IServer>(camelCaseOptions);
        var clientRpcWithPrefix    = clientRpc.Attach <IServer>(prefixOptions);

        clientRpc.StartListening();

        // Construct the server to only respond to one set of method names for now to confirm that the client is sending the right one.
        this.serverRpc = new JsonRpc(this.serverStream, this.serverStream);
        this.serverRpc.AddLocalRpcTarget(this.server, new JsonRpcTargetOptions {
            EventNameTransform = camelCaseOptions.EventNameTransform
        });
        this.serverRpc.StartListening();

        var          tcs     = new TaskCompletionSource <EventArgs>();
        EventHandler handler = (sender, args) => tcs.SetResult(args);

        clientRpcWithCamelCase.ItHappened += handler;
        this.server.OnItHappened(EventArgs.Empty);
        var actualArgs = await tcs.Task.WithCancellation(this.TimeoutToken);

        Assert.NotNull(actualArgs);

        clientRpcWithCamelCase.ItHappened -= handler;
        clientRpcWithPrefix.ItHappened    += handler;
        tcs = new TaskCompletionSource <EventArgs>();
        this.server.OnItHappened(EventArgs.Empty);
        await Assert.ThrowsAsync <TimeoutException>(() => tcs.Task.WithTimeout(ExpectedTimeout));

        Assert.False(tcs.Task.IsCompleted);

        clientRpcWithPrefix.ItHappened -= handler;
    }