|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import division |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import os |
| 7 | +import random |
| 8 | +import signal |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import threading |
| 12 | +import time |
| 13 | +import unittest |
| 14 | + |
| 15 | +import plasma |
| 16 | +import pyarrow as pa |
| 17 | + |
| 18 | +DEFAULT_PLASMA_STORE_MEMORY = 10 ** 9 |
| 19 | + |
| 20 | +USE_VALGRIND = False |
| 21 | + |
| 22 | +def random_name(): |
| 23 | + return str(random.randint(0, 99999999)) |
| 24 | + |
| 25 | +def random_object_id(): |
| 26 | + return plasma.ObjectID(np.random.bytes(20)) |
| 27 | + |
| 28 | +def generate_metadata(length): |
| 29 | + metadata_buffer = bytearray(length) |
| 30 | + if length > 0: |
| 31 | + metadata_buffer[0] = random.randint(0, 255) |
| 32 | + metadata_buffer[-1] = random.randint(0, 255) |
| 33 | + for _ in range(100): |
| 34 | + metadata_buffer[random.randint(0, length - 1)] = random.randint(0, 255) |
| 35 | + return metadata_buffer |
| 36 | + |
| 37 | +def assert_get_object_equal(unit_test, client1, client2, object_id, |
| 38 | + memory_buffer=None, metadata=None): |
| 39 | + client1_buff = client1.get([object_id])[0] |
| 40 | + client2_buff = client2.get([object_id])[0] |
| 41 | + client1_metadata = client1.get_metadata([object_id])[0] |
| 42 | + client2_metadata = client2.get_metadata([object_id])[0] |
| 43 | + unit_test.assertEqual(len(client1_buff), len(client2_buff)) |
| 44 | + unit_test.assertEqual(len(client1_metadata), len(client2_metadata)) |
| 45 | + # Check that the buffers from the two clients are the same. |
| 46 | + unit_test.assertTrue(plasma.buffers_equal(client1_buff, client2_buff)) |
| 47 | + # Check that the metadata buffers from the two clients are the same. |
| 48 | + unit_test.assertTrue(plasma.buffers_equal(client1_metadata, |
| 49 | + client2_metadata)) |
| 50 | + # If a reference buffer was provided, check that it is the same as well. |
| 51 | + if memory_buffer is not None: |
| 52 | + unit_test.assertTrue(plasma.buffers_equal(memory_buffer, client1_buff)) |
| 53 | + # If reference metadata was provided, check that it is the same as well. |
| 54 | + if metadata is not None: |
| 55 | + unit_test.assertTrue(plasma.buffers_equal(metadata, client1_metadata)) |
| 56 | + |
| 57 | +def start_plasma_store(plasma_store_memory=DEFAULT_PLASMA_STORE_MEMORY, |
| 58 | + use_valgrind=False, use_profiler=False, |
| 59 | + stdout_file=None, stderr_file=None): |
| 60 | + """Start a plasma store process. |
| 61 | + Args: |
| 62 | + use_valgrind (bool): True if the plasma store should be started inside of |
| 63 | + valgrind. If this is True, use_profiler must be False. |
| 64 | + use_profiler (bool): True if the plasma store should be started inside a |
| 65 | + profiler. If this is True, use_valgrind must be False. |
| 66 | + stdout_file: A file handle opened for writing to redirect stdout to. If no |
| 67 | + redirection should happen, then this should be None. |
| 68 | + stderr_file: A file handle opened for writing to redirect stderr to. If no |
| 69 | + redirection should happen, then this should be None. |
| 70 | + Return: |
| 71 | + A tuple of the name of the plasma store socket and the process ID of the |
| 72 | + plasma store process. |
| 73 | + """ |
| 74 | + if use_valgrind and use_profiler: |
| 75 | + raise Exception("Cannot use valgrind and profiler at the same time.") |
| 76 | + plasma_store_executable = os.path.join(os.path.abspath( |
| 77 | + os.path.dirname(__file__)), |
| 78 | + "../../../build/debug/plasma_store") |
| 79 | + plasma_store_name = "/tmp/plasma_store{}".format(random_name()) |
| 80 | + command = [plasma_store_executable, |
| 81 | + "-s", plasma_store_name, |
| 82 | + "-m", str(plasma_store_memory)] |
| 83 | + if use_valgrind: |
| 84 | + pid = subprocess.Popen(["valgrind", |
| 85 | + "--track-origins=yes", |
| 86 | + "--leak-check=full", |
| 87 | + "--show-leak-kinds=all", |
| 88 | + "--error-exitcode=1"] + command, |
| 89 | + stdout=stdout_file, stderr=stderr_file) |
| 90 | + time.sleep(1.0) |
| 91 | + elif use_profiler: |
| 92 | + pid = subprocess.Popen(["valgrind", "--tool=callgrind"] + command, |
| 93 | + stdout=stdout_file, stderr=stderr_file) |
| 94 | + time.sleep(1.0) |
| 95 | + else: |
| 96 | + pid = subprocess.Popen(command, stdout=stdout_file, stderr=stderr_file) |
| 97 | + time.sleep(0.1) |
| 98 | + return plasma_store_name, pid |
| 99 | + |
| 100 | +class TestPlasmaClient(unittest.TestCase): |
| 101 | + |
| 102 | + def setUp(self): |
| 103 | + # Start Plasma store. |
| 104 | + plasma_store_name, self.p = start_plasma_store( |
| 105 | + use_valgrind=USE_VALGRIND) |
| 106 | + # Connect to Plasma. |
| 107 | + self.plasma_client = plasma.PlasmaClient() |
| 108 | + self.plasma_client.connect(plasma_store_name, "", 64) |
| 109 | + # For the eviction test |
| 110 | + self.plasma_client2 = plasma.PlasmaClient() |
| 111 | + self.plasma_client2.connect(plasma_store_name, "", 0) |
| 112 | + |
| 113 | + def tearDown(self): |
| 114 | + # Check that the Plasma store is still alive. |
| 115 | + self.assertEqual(self.p.poll(), None) |
| 116 | + # Kill the plasma store process. |
| 117 | + if USE_VALGRIND: |
| 118 | + self.p.send_signal(signal.SIGTERM) |
| 119 | + self.p.wait() |
| 120 | + if self.p.returncode != 0: |
| 121 | + os._exit(-1) |
| 122 | + else: |
| 123 | + self.p.kill() |
| 124 | + |
| 125 | + def test_create(self): |
| 126 | + # Create an object id string. |
| 127 | + object_id = random_object_id() |
| 128 | + # Create a new buffer and write to it. |
| 129 | + length = 50 |
| 130 | + memory_buffer = np.frombuffer(self.plasma_client.create(object_id, length), dtype="uint8") |
| 131 | + for i in range(length): |
| 132 | + memory_buffer[i] = i % 256 |
| 133 | + # Seal the object. |
| 134 | + self.plasma_client.seal(object_id) |
| 135 | + # Get the object. |
| 136 | + memory_buffer = np.frombuffer(self.plasma_client.get([object_id])[0], dtype="uint8") |
| 137 | + for i in range(length): |
| 138 | + self.assertEqual(memory_buffer[i], i % 256) |
| 139 | + |
| 140 | + def test_create_with_metadata(self): |
| 141 | + for length in range(1000): |
| 142 | + # Create an object id string. |
| 143 | + object_id = random_object_id() |
| 144 | + # Create a random metadata string. |
| 145 | + metadata = generate_metadata(length) |
| 146 | + # Create a new buffer and write to it. |
| 147 | + memory_buffer = np.frombuffer(self.plasma_client.create(object_id, length, metadata), dtype="uint8") |
| 148 | + for i in range(length): |
| 149 | + memory_buffer[i] = i % 256 |
| 150 | + # Seal the object. |
| 151 | + self.plasma_client.seal(object_id) |
| 152 | + # Get the object. |
| 153 | + memory_buffer = np.frombuffer(self.plasma_client.get([object_id])[0], dtype="uint8") |
| 154 | + for i in range(length): |
| 155 | + self.assertEqual(memory_buffer[i], i % 256) |
| 156 | + # Get the metadata. |
| 157 | + metadata_buffer = np.frombuffer(self.plasma_client.get_metadata([object_id])[0], dtype="uint8") |
| 158 | + self.assertEqual(len(metadata), len(metadata_buffer)) |
| 159 | + for i in range(len(metadata)): |
| 160 | + self.assertEqual(metadata[i], metadata_buffer[i]) |
| 161 | + |
| 162 | + def test_create_existing(self): |
| 163 | + # This test is partially used to test the code path in which we create an |
| 164 | + # object with an ID that already exists |
| 165 | + length = 100 |
| 166 | + for _ in range(1000): |
| 167 | + object_id = random_object_id() |
| 168 | + self.plasma_client.create(object_id, length, generate_metadata(length)) |
| 169 | + try: |
| 170 | + self.plasma_client.create(object_id, length, generate_metadata(length)) |
| 171 | + except pa.lib.ArrowException as e: |
| 172 | + pass |
| 173 | + else: |
| 174 | + self.assertTrue(False) |
| 175 | + |
| 176 | +if __name__ == "__main__": |
| 177 | + if len(sys.argv) > 1: |
| 178 | + # Pop the argument so we don't mess with unittest's own argument parser. |
| 179 | + if sys.argv[-1] == "valgrind": |
| 180 | + arg = sys.argv.pop() |
| 181 | + USE_VALGRIND = True |
| 182 | + print("Using valgrind for tests") |
| 183 | + unittest.main(verbosity=2) |
0 commit comments