xref: /aosp_15_r20/external/pigweed/pw_transfer/integration_test/multi_transfer_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1#!/usr/bin/env python3
2# Copyright 2022 The Pigweed Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15"""Cross-language pw_transfer tests that do several transfers per session.
16
17Usage:
18
19   bazel run pw_transfer/integration_test:multi_transfer_test
20
21Command-line arguments must be provided after a double-dash:
22
23   bazel run pw_transfer/integration_test:multi_transfer_test -- \
24       --server-port 3304
25
26Which tests to run can be specified as command-line arguments:
27
28  bazel run pw_transfer/integration_test:multi_transfer_test -- \
29      MultiTransferIntegrationTest.test_write_to_same_id_1_java
30
31"""
32
33from parameterized import parameterized
34import random
35
36from pw_transfer.integration_test import test_fixture
37from test_fixture import TransferIntegrationTestHarness, BasicTransfer
38from pw_transfer.integration_test import config_pb2
39
40
41class MultiTransferIntegrationTest(test_fixture.TransferIntegrationTest):
42    # Each set of transfer tests uses a different client/server port pair to
43    # allow tests to be run in parallel.
44    HARNESS_CONFIG = TransferIntegrationTestHarness.Config(
45        server_port=3308, client_port=3309
46    )
47
48    @parameterized.expand(
49        [
50            ("cpp"),
51            ("java"),
52            ("python"),
53        ]
54    )
55    def test_write_to_same_id(self, client_type):
56        rng = random.Random(1533659510898)
57        config = self.default_config()
58        resource_id = 5
59        transfers: list[BasicTransfer] = []
60        for i in range(1, 6):
61            transfers.append(
62                BasicTransfer(
63                    id=resource_id,
64                    type=config_pb2.TransferAction.TransferType.WRITE_TO_SERVER,
65                    data=rng.randbytes(rng.randrange(213, 1111)),
66                )
67            )
68
69        self.do_basic_transfer_sequence(client_type, config, transfers)
70
71    @parameterized.expand(
72        [
73            ("cpp"),
74            ("java"),
75            ("python"),
76        ]
77    )
78    def test_read_from_same_id(self, client_type):
79        rng = random.Random(1533659510898)
80        config = self.default_config()
81        resource_id = 5
82        transfers: list[BasicTransfer] = []
83        for i in range(1, 6):
84            transfers.append(
85                BasicTransfer(
86                    id=resource_id,
87                    type=config_pb2.TransferAction.TransferType.READ_FROM_SERVER,
88                    data=rng.randbytes(rng.randrange(213, 1111)),
89                )
90            )
91
92        self.do_basic_transfer_sequence(client_type, config, transfers)
93
94    @parameterized.expand(
95        [
96            ("cpp"),
97            ("java"),
98            ("python"),
99        ]
100    )
101    def test_read_write_with_same_id(self, client_type):
102        rng = random.Random(1533659510898)
103        config = self.default_config()
104        resource_id = 53333333
105        transfers: list[BasicTransfer] = []
106        for i in range(1, 6):
107            transfer_type = (
108                config_pb2.TransferAction.TransferType.READ_FROM_SERVER
109                if i % 2 == 0
110                else config_pb2.TransferAction.TransferType.WRITE_TO_SERVER
111            )
112            transfers.append(
113                BasicTransfer(
114                    id=resource_id,
115                    type=config_pb2.TransferAction.TransferType.READ_FROM_SERVER,
116                    data=rng.randbytes(rng.randrange(213, 1111)),
117                )
118            )
119
120        self.do_basic_transfer_sequence(client_type, config, transfers)
121
122
123if __name__ == '__main__':
124    test_fixture.run_tests_for(MultiTransferIntegrationTest)
125