|
| 1 | +# Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions |
| 5 | +# are met: |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# * Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | +# contributors may be used to endorse or promote products derived |
| 13 | +# from this software without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +import os |
| 28 | +import time |
| 29 | +import unittest |
| 30 | + |
| 31 | +import numpy as np |
| 32 | +import tritonclient.grpc as grpcclient |
| 33 | + |
| 34 | + |
| 35 | +class ResponseSenderTest(unittest.TestCase): |
| 36 | + def _generate_streaming_callback_and_responses_pair(self): |
| 37 | + responses = [] # [{"result": result, "error": error}, ...] |
| 38 | + |
| 39 | + def callback(result, error): |
| 40 | + responses.append({"result": result, "error": error}) |
| 41 | + |
| 42 | + return callback, responses |
| 43 | + |
| 44 | + def test_respond_after_complete_final(self): |
| 45 | + with open(os.environ["SERVER_LOG"]) as f: |
| 46 | + server_log = f.read() |
| 47 | + self.assertNotIn("Test Passed", server_log) |
| 48 | + |
| 49 | + model_name = "response_sender_complete_final" |
| 50 | + shape = [1, 1] |
| 51 | + inputs = [grpcclient.InferInput("INPUT0", shape, "FP32")] |
| 52 | + input0_np = np.array([[123.45]], np.float32) |
| 53 | + inputs[0].set_data_from_numpy(input0_np) |
| 54 | + |
| 55 | + callback, responses = self._generate_streaming_callback_and_responses_pair() |
| 56 | + with grpcclient.InferenceServerClient("localhost:8001") as client: |
| 57 | + client.start_stream(callback) |
| 58 | + client.async_stream_infer(model_name, inputs) |
| 59 | + client.stop_stream() |
| 60 | + |
| 61 | + self.assertEqual(len(responses), 1) |
| 62 | + for response in responses: |
| 63 | + output0_np = response["result"].as_numpy(name="OUTPUT0") |
| 64 | + self.assertTrue(np.allclose(input0_np, output0_np)) |
| 65 | + self.assertIsNone(response["error"]) |
| 66 | + |
| 67 | + time.sleep(1) # make sure the logs are written before checking |
| 68 | + with open(os.environ["SERVER_LOG"]) as f: |
| 69 | + server_log = f.read() |
| 70 | + self.assertNotIn("Unexpected request length", server_log) |
| 71 | + self.assertNotIn("Expected exception not raised", server_log) |
| 72 | + self.assertNotIn("Test FAILED", server_log) |
| 73 | + self.assertIn("Test Passed", server_log) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + unittest.main() |
0 commit comments