From 85af85fdd9a59253800921802caedd4c9ae1489f Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Fri, 5 Jul 2019 13:46:42 -0400
Subject: [PATCH] Factor out the share writing duplication

---
 .../tests/test_storage_protocol.py            | 51 ++++++++++++++-----
 1 file changed, 39 insertions(+), 12 deletions(-)

diff --git a/src/_secureaccesstokenauthorizer/tests/test_storage_protocol.py b/src/_secureaccesstokenauthorizer/tests/test_storage_protocol.py
index b4f341b..5cb8a06 100644
--- a/src/_secureaccesstokenauthorizer/tests/test_storage_protocol.py
+++ b/src/_secureaccesstokenauthorizer/tests/test_storage_protocol.py
@@ -203,7 +203,8 @@ class ImmutableTests(TestCase):
         assume(add_lease_secret != renew_lease_secret)
 
         # Create a share we can toy with.
-        _, allocated = self.anonymous_storage_server.remote_allocate_buckets(
+        write_toy_shares(
+            self.anonymous_storage_server,
             storage_index,
             add_lease_secret,
             cancel_secret,
@@ -211,9 +212,6 @@ class ImmutableTests(TestCase):
             size,
             canary=self.canary,
         )
-        [(_, writer)] = allocated.items()
-        writer.remote_write(0, bytes_for_share(sharenum, size))
-        writer.remote_close()
 
         extract_result(
             self.client.add_lease(
@@ -246,7 +244,8 @@ class ImmutableTests(TestCase):
         self.useFixture(MonkeyPatch("time.time", lambda: now))
 
         # Create a share we can toy with.
-        _, allocated = self.anonymous_storage_server.remote_allocate_buckets(
+        write_toy_shares(
+            self.anonymous_storage_server,
             storage_index,
             renew_secret,
             cancel_secret,
@@ -254,9 +253,6 @@ class ImmutableTests(TestCase):
             size,
             canary=self.canary,
         )
-        [(_, writer)] = allocated.items()
-        writer.remote_write(0, bytes_for_share(sharenum, size))
-        writer.remote_close()
 
         now += 100000
         extract_result(
@@ -291,7 +287,8 @@ class ImmutableTests(TestCase):
         cleanup_storage_server(self.anonymous_storage_server)
 
         # Create a share we can toy with.
-        _, allocated = self.anonymous_storage_server.remote_allocate_buckets(
+        write_toy_shares(
+            self.anonymous_storage_server,
             storage_index,
             renew_secret,
             cancel_secret,
@@ -299,9 +296,6 @@ class ImmutableTests(TestCase):
             size,
             canary=self.canary,
         )
-        [(_, writer)] = allocated.items()
-        writer.remote_write(0, bytes_for_share(sharenum, size))
-        writer.remote_close()
 
         extract_result(
             self.client.advise_corrupt_share(
@@ -317,6 +311,39 @@ class ImmutableTests(TestCase):
         )
 
 
+def write_toy_shares(
+        storage_server,
+        storage_index,
+        renew_secret,
+        cancel_secret,
+        sharenums,
+        size,
+        canary,
+):
+    """
+    Write some immutable shares to the given storage server.
+
+    :param allmydata.storage.server.StorageServer storage_server:
+    :param bytes storage_index:
+    :param bytes renew_secret:
+    :param bytes cancel_secret:
+    :param set[int] sharenums:
+    :param int size:
+    :param IRemoteReference canary:
+    """
+    _, allocated = storage_server.remote_allocate_buckets(
+        storage_index,
+        renew_secret,
+        cancel_secret,
+        sharenums,
+        size,
+        canary=canary,
+    )
+    for (sharenum, writer) in allocated.items():
+        writer.remote_write(0, bytes_for_share(sharenum, size))
+        writer.remote_close()
+
+
 def get_leases(storage_server, storage_index):
     """
     Get all leases for all shares of the given storage index on the given
-- 
GitLab