|
| 1 | +package io.temporal.internal.statemachines; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotEquals; |
| 5 | +import static org.junit.Assert.assertSame; |
| 6 | + |
| 7 | +import com.google.common.io.BaseEncoding; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Random; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public class WorkflowRandomStreamsTest { |
| 14 | + private static final String RUN_ID = "runID"; |
| 15 | + private static final String NAME = "io.temporal.test"; |
| 16 | + |
| 17 | + @Test |
| 18 | + public void deriveSeed() { |
| 19 | + WorkflowRandomStreams randoms = new WorkflowRandomStreams(); |
| 20 | + long seed = randoms.deriveSeed(RUN_ID, NAME); |
| 21 | + assertNotEquals(seed, randoms.deriveSeed("other", NAME)); |
| 22 | + assertNotEquals(seed, randoms.deriveSeed(RUN_ID, "other")); |
| 23 | + assertNotEquals(seed, randoms.deriveSeed("other", "other")); |
| 24 | + } |
| 25 | + |
| 26 | + /** Pins the seed derivation and resulting byte stream. Changing either breaks replay. */ |
| 27 | + @Test |
| 28 | + public void getRandomStreamGolden() { |
| 29 | + WorkflowRandomStreams randoms = new WorkflowRandomStreams(); |
| 30 | + assertEquals(8181915698088084985L, randoms.deriveSeed(RUN_ID, NAME)); |
| 31 | + |
| 32 | + byte[] bytes = new byte[32]; |
| 33 | + randoms.get(RUN_ID, NAME).nextBytes(bytes); |
| 34 | + assertEquals( |
| 35 | + "1c1d4dd36999ff851d72aa41f660ecd3220d83499109ba3ba24e455a1b776c21", |
| 36 | + BaseEncoding.base16().lowerCase().encode(bytes)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void deriveSeedSeparators() { |
| 41 | + WorkflowRandomStreams randoms = new WorkflowRandomStreams(); |
| 42 | + assertNotEquals(randoms.deriveSeed("ab", "c"), randoms.deriveSeed("a", "bc")); |
| 43 | + } |
| 44 | + |
| 45 | + /** A second lookup under the same name continues the sequence rather than restarting it. */ |
| 46 | + @Test |
| 47 | + public void getRandomStreamMemoizes() { |
| 48 | + WorkflowRandomStreams randoms = new WorkflowRandomStreams(); |
| 49 | + |
| 50 | + Random first = randoms.get(RUN_ID, NAME); |
| 51 | + long firstDraw = first.nextLong(); |
| 52 | + |
| 53 | + Random second = randoms.get(RUN_ID, NAME); |
| 54 | + long secondDraw = second.nextLong(); |
| 55 | + |
| 56 | + assertSame(first, second); |
| 57 | + assertNotEquals(firstDraw, secondDraw); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Interleaving draws across two names yields the same sequence per name as drawing from each on |
| 62 | + * its own, so how often a workflow draws from one name cannot shift another. |
| 63 | + */ |
| 64 | + @Test |
| 65 | + public void getRandomStreamNamesAreIndependent() { |
| 66 | + WorkflowRandomStreams randoms = new WorkflowRandomStreams(); |
| 67 | + Random first = randoms.get(RUN_ID, NAME); |
| 68 | + Random second = randoms.get(RUN_ID, "other"); |
| 69 | + |
| 70 | + List<Long> interleavedA = new ArrayList<>(); |
| 71 | + List<Long> interleavedB = new ArrayList<>(); |
| 72 | + for (int i = 0; i < 3; i++) { |
| 73 | + interleavedA.add(first.nextLong()); |
| 74 | + interleavedB.add(second.nextLong()); |
| 75 | + } |
| 76 | + |
| 77 | + assertEquals(solo(NAME, 3), interleavedA); |
| 78 | + assertEquals(solo("other", 3), interleavedB); |
| 79 | + assertNotEquals(interleavedA, interleavedB); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void reseedRandomsInPlace() { |
| 84 | + WorkflowRandomStreams randoms = new WorkflowRandomStreams(); |
| 85 | + |
| 86 | + Random first = randoms.get(RUN_ID, NAME); |
| 87 | + randoms.reseed("other"); |
| 88 | + Random second = randoms.get("other", NAME); |
| 89 | + |
| 90 | + assertSame(first, second); |
| 91 | + assertEquals(new WorkflowRandomStreams().get("other", NAME).nextLong(), second.nextLong()); |
| 92 | + } |
| 93 | + |
| 94 | + private static List<Long> solo(String name, int draws) { |
| 95 | + Random random = new WorkflowRandomStreams().get(RUN_ID, name); |
| 96 | + List<Long> result = new ArrayList<>(); |
| 97 | + for (int i = 0; i < draws; i++) { |
| 98 | + result.add(random.nextLong()); |
| 99 | + } |
| 100 | + return result; |
| 101 | + } |
| 102 | +} |
0 commit comments