mirror of
https://github.com/unclechu/gRPC-haskell.git
synced 2024-11-23 11:39:43 +01:00
Merge pull request #6 from awakenetworks/joel-fix-python-client-test-hang
Fix hang in Python test client
This commit is contained in:
commit
584ae318a9
1 changed files with 60 additions and 57 deletions
|
@ -8,71 +8,74 @@ print "Starting python client"
|
||||||
channel = beta_implementations.insecure_channel('localhost', 50051)
|
channel = beta_implementations.insecure_channel('localhost', 50051)
|
||||||
stub = beta_create_SimpleService_stub(channel)
|
stub = beta_create_SimpleService_stub(channel)
|
||||||
|
|
||||||
# Test normal call: return a sum of all numbers sent to it
|
def runTests():
|
||||||
print "Test 100 random sums"
|
# Test normal call: return a sum of all numbers sent to it
|
||||||
for i in xrange(100):
|
print "Test 100 random sums"
|
||||||
randints = [random.randint(0, 1000) for _ in xrange(random.randint(10, 1000))]
|
for i in xrange(100):
|
||||||
name = "test%d" % i
|
randints = [random.randint(0, 1000) for _ in xrange(random.randint(10, 1000))]
|
||||||
response = stub.normalCall(SimpleServiceRequest(request = name, num = randints), 10)
|
name = "test%d" % i
|
||||||
|
response = stub.normalCall(SimpleServiceRequest(request = name, num = randints), 10)
|
||||||
|
assert response.response == name
|
||||||
|
assert response.num == sum(randints)
|
||||||
|
|
||||||
assert response.response == name
|
# Test streaming call: The server response will be the sum of all numbers sent in the request along with a concatenation of the request name
|
||||||
assert response.num == sum(randints)
|
print "Test 100 random sums (client streaming)"
|
||||||
|
for i in xrange(100):
|
||||||
|
# avoid globals
|
||||||
|
expected_sum = [0]
|
||||||
|
expected_response_name = ['']
|
||||||
|
def send_requests(esum, ename):
|
||||||
|
for _ in xrange(random.randint(5, 50)):
|
||||||
|
nums = [random.randint(0, 1000) for _ in xrange(random.randint(10, 100))]
|
||||||
|
name = str(uuid4())
|
||||||
|
esum[0] += sum(nums)
|
||||||
|
ename[0] += name
|
||||||
|
yield SimpleServiceRequest(request = name, num = nums)
|
||||||
|
response = stub.clientStreamingCall(send_requests(expected_sum, expected_response_name), 10)
|
||||||
|
assert response.response == expected_response_name[0]
|
||||||
|
assert response.num == expected_sum[0]
|
||||||
|
|
||||||
# Test streaming call: The server response will be the sum of all numbers sent in the request along with a concatenation of the request name
|
# Test server streaming call: The server should respond once for each number in the request
|
||||||
print "Test 100 random sums (client streaming)"
|
print "Test 100 random server streaming calls"
|
||||||
for i in xrange(100):
|
for i in xrange(100):
|
||||||
expected_sum = 0
|
nums = [random.randint(0, 1000) for _ in xrange(random.randint(0, 1000))]
|
||||||
expected_response_name = ''
|
|
||||||
|
|
||||||
def send_requests():
|
for response in stub.serverStreamingCall(SimpleServiceRequest(request = "server streaming", num = nums), 60):
|
||||||
global expected_sum
|
assert response.num == nums[0]
|
||||||
global expected_response_name
|
assert response.response == "server streaming"
|
||||||
|
nums = nums[1:]
|
||||||
|
|
||||||
for _ in xrange(random.randint(5, 50)):
|
# Test bidirectional streaming: for each request, we should get a response indicating the sum of all numbers sent in the last request
|
||||||
nums = [random.randint(0, 1000) for _ in xrange(random.randint(10, 100))]
|
print "Test bidirectional streaming"
|
||||||
name = str(uuid4())
|
for i in xrange(100):
|
||||||
|
requests = Queue.Queue()
|
||||||
|
def send_requests():
|
||||||
|
for _ in xrange(random.randint(5, 50)):
|
||||||
|
nums = [random.randint(0, 1000) for _ in xrange(random.randint(10, 100))]
|
||||||
|
name = str(uuid4())
|
||||||
|
|
||||||
expected_sum += sum(nums)
|
requests.put((name, sum(nums)))
|
||||||
expected_response_name += name
|
|
||||||
|
|
||||||
yield SimpleServiceRequest(request = name, num = nums)
|
yield SimpleServiceRequest(request = name, num = nums)
|
||||||
|
|
||||||
response = stub.clientStreamingCall(send_requests(), 10)
|
for response in stub.biDiStreamingCall(send_requests(), 10):
|
||||||
assert response.response == expected_response_name
|
(exp_name, exp_sum) = requests.get()
|
||||||
assert response.num == expected_sum
|
|
||||||
|
|
||||||
# Test server streaming call: The server should respond once for each number in the request
|
assert response.response == exp_name
|
||||||
print "Test 100 random server streaming calls"
|
assert response.num == exp_sum
|
||||||
for i in xrange(100):
|
|
||||||
nums = [random.randint(0, 1000) for _ in xrange(random.randint(0, 1000))]
|
|
||||||
|
|
||||||
for response in stub.serverStreamingCall(SimpleServiceRequest(request = "server streaming", num = nums), 60):
|
print "Sending DONE message to server"
|
||||||
assert response.num == nums[0]
|
stub.done(SimpleServiceDone(), 10)
|
||||||
assert response.response == "server streaming"
|
|
||||||
nums = nums[1:]
|
|
||||||
|
|
||||||
# Test bidirectional streaming: for each request, we should get a response indicating the sum of all numbers sent in the last request
|
print "All python client tests against the generated server were successful"
|
||||||
print "Test bidirectional streaming"
|
|
||||||
for i in xrange(100):
|
|
||||||
requests = Queue.Queue()
|
|
||||||
def send_requests():
|
|
||||||
global cur_request
|
|
||||||
global cur_nums
|
|
||||||
global requests
|
|
||||||
|
|
||||||
for _ in xrange(random.randint(5, 50)):
|
try:
|
||||||
nums = [random.randint(0, 1000) for _ in xrange(random.randint(10, 100))]
|
runTests()
|
||||||
name = str(uuid4())
|
except Exception as e:
|
||||||
|
print "Python client test exception caught: "
|
||||||
requests.put((name, sum(nums)))
|
print e.__doc__
|
||||||
|
print e.message
|
||||||
yield SimpleServiceRequest(request = name, num = nums)
|
print "Sending DONE message to server before exiting"
|
||||||
|
stub.done(SimpleServiceDone(), 10)
|
||||||
for response in stub.biDiStreamingCall(send_requests(), 10):
|
print "Exiting with failure status"
|
||||||
(exp_name, exp_sum) = requests.get()
|
exit(1)
|
||||||
|
|
||||||
assert response.response == exp_name
|
|
||||||
assert response.num == exp_sum
|
|
||||||
|
|
||||||
# Signal the ending of the test
|
|
||||||
stub.done(SimpleServiceDone(), 10)
|
|
||||||
|
|
Loading…
Reference in a new issue