|
| 1 | +require_relative '../spec_helpers' |
| 2 | +require 'krpc/types' |
| 3 | + |
| 4 | + |
| 5 | +describe KRPC::Streaming do |
| 6 | + include_context "test client support" |
| 7 | + |
| 8 | + def expect_equal_allow_delay(actual_getter, expected, retry_delay=0.1, max_retries=16) |
| 9 | + i = 0 |
| 10 | + while i<max_retries do |
| 11 | + i += 1 |
| 12 | + return if actual_getter.call == expected |
| 13 | + sleep retry_delay |
| 14 | + end |
| 15 | + expect(actual_getter).to eq expected |
| 16 | + end |
| 17 | + |
| 18 | + |
| 19 | + specify "error handling" do |
| 20 | + expect { @test_service.throw_argument_exception_stream }.to raise_error(KRPC::RPCError, "Invalid argument") |
| 21 | + expect { @test_service.throw_invalid_operation_exception_stream }.to raise_error(KRPC::RPCError, "Invalid operation") |
| 22 | + end |
| 23 | + |
| 24 | + specify "value parameters handling" do |
| 25 | + expect(@test_service.float_to_string_stream(3.14159).get).to match(/3[\.,]14159/) |
| 26 | + end |
| 27 | + |
| 28 | + specify "multiple value parameters handling" do |
| 29 | + expect(@test_service.add_multiple_values_stream(0.14159, 1, 2).value).to match(/3[\.,]14159/) |
| 30 | + end |
| 31 | + |
| 32 | + specify "incorrect parameter type handling" do |
| 33 | + expect { @test_service.float_to_string_stream("foo") }.to raise_error(KRPC::ArgumentErrorSig) |
| 34 | + end |
| 35 | + |
| 36 | + specify "properties handling" do |
| 37 | + @test_service.string_property = "foo" |
| 38 | + stream = @test_service.string_property_stream |
| 39 | + expect(stream.get).to eq "foo" |
| 40 | + @test_service.string_property = "bar" |
| 41 | + expect_equal_allow_delay(lambda{stream.get}, "bar") |
| 42 | + obj1 = @test_service.create_test_object("bar1") |
| 43 | + obj2 = @test_service.create_test_object("bar2") |
| 44 | + @test_service.object_property = obj1 |
| 45 | + stream2 = @test_service.object_property_stream |
| 46 | + expect(stream2.get).to eq obj1 |
| 47 | + @test_service.object_property = obj2 |
| 48 | + expect_equal_allow_delay(lambda{stream2.get}, obj2) |
| 49 | + end |
| 50 | + |
| 51 | + specify "named parameters handling" do |
| 52 | + obj = @test_service.create_test_object("jeb") |
| 53 | + expect(obj.optional_arguments_stream(z: "1", x: "2", another_parameter: "3", y: "4").get).to eq "2413" |
| 54 | + expect(obj.optional_arguments_stream("1", "2", another_parameter: "3").get).to eq "12bar3" |
| 55 | + end |
| 56 | + |
| 57 | + specify "KRPC::Streaming::Stream object info" do |
| 58 | + obj = @test_service.create_test_object("bob") |
| 59 | + stream = obj.float_to_string_stream(3.14159) |
| 60 | + expect(stream.method).to eq obj.method(:float_to_string) |
| 61 | + expect(stream.args).to eq [3.14159] |
| 62 | + expect(stream.kwargs).to eq ({}) |
| 63 | + expect(stream.return_type).to eq KRPC::TypeStore["string"] |
| 64 | + |
| 65 | + stream2 = @test_service.optional_arguments_stream(z: "bob", x: "foo") |
| 66 | + expect(stream2.method).to eq @test_service.method(:optional_arguments) |
| 67 | + expect(stream2.args).to eq [] |
| 68 | + expect(stream2.kwargs).to eq ({z: "bob", x: "foo"}) |
| 69 | + expect(stream2.return_type).to eq KRPC::TypeStore["string"] |
| 70 | + end |
| 71 | + |
| 72 | + specify "KRPC::Streaming::Stream#active?" do |
| 73 | + stream = @test_service.optional_arguments_stream(5) |
| 74 | + stream2 = @test_service.optional_arguments_stream(6) |
| 75 | + expect(stream.active?).to be true |
| 76 | + expect(stream2.active?).to be true |
| 77 | + stream.close |
| 78 | + expect(stream.active?).to be false |
| 79 | + expect(stream2.active?).to be true |
| 80 | + stream.close |
| 81 | + stream.remove |
| 82 | + expect(stream.active?).to be false |
| 83 | + expect(stream2.active?).to be true |
| 84 | + stream2.remove |
| 85 | + expect(stream.active?).to be false |
| 86 | + expect(stream2.active?).to be false |
| 87 | + end |
| 88 | + |
| 89 | + specify "Streams disconnect when client disconnects" do |
| 90 | + stream = @test_service.optional_arguments_stream(3) |
| 91 | + stream2 = @test_service.optional_arguments_stream(4) |
| 92 | + stream.close |
| 93 | + expect(stream.active?).to be false |
| 94 | + expect(stream2.active?).to be true |
| 95 | + @test_client.close |
| 96 | + expect(stream.active?).to be false |
| 97 | + expect(stream2.active?).to be false |
| 98 | + @test_client.connect |
| 99 | + expect(stream.active?).to be false |
| 100 | + expect(stream2.active?).to be false |
| 101 | + end |
| 102 | + |
| 103 | +end |
| 104 | + |
0 commit comments