|
39 | 39 | import com.google.api.client.testing.http.MockLowLevelHttpResponse; |
40 | 40 | import com.google.api.client.util.Clock; |
41 | 41 | import com.google.auth.http.HttpTransportFactory; |
| 42 | +import java.io.ByteArrayInputStream; |
| 43 | +import java.io.ByteArrayOutputStream; |
| 44 | +import java.io.ObjectInputStream; |
| 45 | +import java.io.ObjectOutputStream; |
42 | 46 | import java.util.Collections; |
43 | 47 | import java.util.concurrent.atomic.AtomicLong; |
44 | 48 | import org.junit.After; |
@@ -99,6 +103,30 @@ public void testShouldRefresh() { |
99 | 103 | assertFalse(rab.isExpired()); |
100 | 104 | } |
101 | 105 |
|
| 106 | + @Test |
| 107 | + public void testSerialization() throws Exception { |
| 108 | + long now = testClock.currentTimeMillis(); |
| 109 | + RegionalAccessBoundary rab = |
| 110 | + new RegionalAccessBoundary("encoded", Collections.singletonList("loc"), now, testClock); |
| 111 | + |
| 112 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 113 | + ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 114 | + oos.writeObject(rab); |
| 115 | + oos.close(); |
| 116 | + |
| 117 | + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); |
| 118 | + ObjectInputStream ois = new ObjectInputStream(bais); |
| 119 | + RegionalAccessBoundary deserializedRab = (RegionalAccessBoundary) ois.readObject(); |
| 120 | + ois.close(); |
| 121 | + |
| 122 | + assertEquals("encoded", deserializedRab.getEncodedLocations()); |
| 123 | + assertEquals(1, deserializedRab.getLocations().size()); |
| 124 | + assertEquals("loc", deserializedRab.getLocations().get(0)); |
| 125 | + // The transient clock field should be restored to Clock.SYSTEM upon deserialization, |
| 126 | + // thereby avoiding a NullPointerException when checking expiration. |
| 127 | + assertFalse(deserializedRab.isExpired()); |
| 128 | + } |
| 129 | + |
102 | 130 | @Test |
103 | 131 | public void testManagerTriggersRefreshInGracePeriod() throws InterruptedException { |
104 | 132 | final String url = |
|
0 commit comments