|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2016 Azamshul Azizy |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 7 | + * associated documentation files (the "Software"), to deal in the Software without restriction, |
| 8 | + * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| 9 | + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 13 | + * substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
| 16 | + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | +package io.azam.ulidj; |
| 22 | + |
| 23 | +import java.security.SecureRandom; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Random; |
| 29 | + |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + * Test class for {@link io.azam.ulidj.MonotonicULID} |
| 35 | + * |
| 36 | + * @author azam |
| 37 | + * @since 1.0.3 |
| 38 | + */ |
| 39 | +public class MonotonicULIDTest { |
| 40 | + @Test |
| 41 | + public void testConstructor() { |
| 42 | + Assert.assertNotNull(new MonotonicULID()); |
| 43 | + Assert.assertNotNull(new MonotonicULID(new Random())); |
| 44 | + Assert.assertNotNull(new MonotonicULID(new SecureRandom())); |
| 45 | + } |
| 46 | + |
| 47 | + @Test(expected = IllegalArgumentException.class) |
| 48 | + public void testConstructorNullRandom() { |
| 49 | + new MonotonicULID(null); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testGenerate() { |
| 54 | + MonotonicULID ulid = new MonotonicULID(); |
| 55 | + String id = ulid.generate(); |
| 56 | + Assert.assertNotNull(id); |
| 57 | + Assert.assertTrue(ULID.isValid(id)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testGenerateConcurrent() { |
| 62 | + MonotonicULID ulid = new MonotonicULID(); |
| 63 | + boolean hasSameTimestamp = false; |
| 64 | + // This test might not end if we cannot generate multiple ULID in the same |
| 65 | + // milliseconds. Unless we are running on really slow CPU, we should be OK. |
| 66 | + while (!hasSameTimestamp) { |
| 67 | + List<String> values = new ArrayList<String>(); |
| 68 | + // Generate a bunch of ULIDS |
| 69 | + // Values are inserted in order |
| 70 | + for (int i = 0; i < 10000; i++) { |
| 71 | + values.add(ulid.generate()); |
| 72 | + } |
| 73 | + // Group into timestamp bucket |
| 74 | + Map<Long, List<byte[]>> groups = new HashMap<Long, List<byte[]>>(); |
| 75 | + for (String value : values) { |
| 76 | + Assert.assertNotNull(value); |
| 77 | + Assert.assertTrue(ULID.isValid(value)); |
| 78 | + long ts = ULID.getTimestamp(value); |
| 79 | + byte[] entropy = ULID.getEntropy(value); |
| 80 | + if (!groups.containsKey(ts)) { |
| 81 | + groups.put(ts, new ArrayList<byte[]>()); |
| 82 | + } |
| 83 | + groups.get(ts).add(entropy); |
| 84 | + } |
| 85 | + // For each timestamp bucket check if entropy is monotonic |
| 86 | + for (long ts : groups.keySet()) { |
| 87 | + // Loop until we have a bucket of 5 ids on the same timestamp |
| 88 | + if (groups.get(ts).size() > 4) { |
| 89 | + // Escape loop on next while eval |
| 90 | + hasSameTimestamp = true; |
| 91 | + List<byte[]> bucketValues = groups.get(ts); |
| 92 | + // Values are inserted in order so we don't have to sort |
| 93 | + byte[] prev = bucketValues.get(0); |
| 94 | + for (int i = 1; i < bucketValues.size(); i++) { |
| 95 | + byte[] curr = bucketValues.get(i); |
| 96 | + // The next value on the same timestamp is an increment of 1-bit if the previous |
| 97 | + // value |
| 98 | + Assert.assertArrayEquals(incrementBytes(prev), curr); |
| 99 | + prev = curr; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Test(expected = IllegalStateException.class) |
| 107 | + public void testGenerateOverflow() { |
| 108 | + // Using a random generator that always return 0xff... so that next increment on |
| 109 | + // the same timestamp will throw exception |
| 110 | + MonotonicULID ulid = new MonotonicULID(new FixedRandom(new byte[] { // |
| 111 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, // |
| 112 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff // |
| 113 | + })); |
| 114 | + List<String> values = new ArrayList<String>(); |
| 115 | + // |
| 116 | + for (int i = 0; i < 1000000; i++) { |
| 117 | + values.add(ulid.generate()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testIncrementBytes() { |
| 123 | + Assert.assertArrayEquals(new byte[] { // |
| 124 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // |
| 125 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 // |
| 126 | + }, incrementBytes(new byte[] { // |
| 127 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // |
| 128 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 // |
| 129 | + })); |
| 130 | + Assert.assertArrayEquals(new byte[] { // |
| 131 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // |
| 132 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff // |
| 133 | + }, incrementBytes(new byte[] { // |
| 134 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // |
| 135 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xfe // |
| 136 | + })); |
| 137 | + Assert.assertArrayEquals(new byte[] { // |
| 138 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // |
| 139 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00 // |
| 140 | + }, incrementBytes(new byte[] { // |
| 141 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // |
| 142 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff // |
| 143 | + })); |
| 144 | + Assert.assertArrayEquals(new byte[] { // |
| 145 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, // |
| 146 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff // |
| 147 | + }, incrementBytes(new byte[] { // |
| 148 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, // |
| 149 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xfe // |
| 150 | + })); |
| 151 | + Assert.assertArrayEquals(null, incrementBytes(new byte[] { // |
| 152 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, // |
| 153 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff // |
| 154 | + })); |
| 155 | + } |
| 156 | + |
| 157 | + byte[] incrementBytes(byte[] bytes) { |
| 158 | + if (bytes == null || bytes.length != ULID.ENTROPY_LENGTH) |
| 159 | + return null; |
| 160 | + byte[] value = new byte[ULID.ENTROPY_LENGTH]; |
| 161 | + |
| 162 | + boolean carry = true; |
| 163 | + for (int i = ULID.ENTROPY_LENGTH - 1; i >= 0; i--) { |
| 164 | + byte work = bytes[i]; |
| 165 | + if (carry) { |
| 166 | + work = (byte) (work + 0x01); |
| 167 | + carry = bytes[i] == (byte) 0xff && carry; |
| 168 | + } |
| 169 | + value[i] = work; |
| 170 | + } |
| 171 | + // Last byte has carry over |
| 172 | + if (carry) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + return value; |
| 177 | + } |
| 178 | + |
| 179 | + class FixedRandom extends Random { |
| 180 | + private final byte[] bytes; |
| 181 | + |
| 182 | + public FixedRandom(byte[] bytes) { |
| 183 | + this.bytes = bytes; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void nextBytes(byte[] out) { |
| 188 | + for (int i = 0; i < out.length; i++) { |
| 189 | + if (i < this.bytes.length) |
| 190 | + out[i] = this.bytes[i]; |
| 191 | + else |
| 192 | + out[i] = 0x00; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | +} |
0 commit comments