| 1 | package snowflake_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "sync" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/kankanreno/go-snowflake" |
| 10 | ) |
| 11 | |
| 12 | func TestID(t *testing.T) { |
| 13 | id := snowflake.ID() |
| 14 | |
| 15 | if id <= 0 { |
| 16 | t.Error("The snowflake should't < 0.") |
| 17 | } |
| 18 | |
| 19 | mp := make(map[uint64]bool) |
| 20 | for i := 0; i < 1000000; i++ { |
| 21 | id, e := snowflake.NextID() |
| 22 | if e != nil { |
| 23 | t.Error(e) |
| 24 | continue |
| 25 | } |
| 26 | if _, ok := mp[id]; ok { |
| 27 | t.Error("ID should't repeat", id) |
| 28 | break |
| 29 | } |
| 30 | mp[id] = true |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestID_bitch(t *testing.T) { |
| 35 | le := 100000 |
| 36 | ch := make(chan uint64, le) |
| 37 | var wg sync.WaitGroup |
| 38 | for i := 0; i < le; i++ { |
| 39 | wg.Add(1) |
| 40 | go func() { |
| 41 | defer wg.Done() |
| 42 | id := snowflake.ID() |
| 43 | ch <- id |
| 44 | }() |
| 45 | } |
| 46 | wg.Wait() |
| 47 | close(ch) |
| 48 | |
| 49 | mp := make(map[uint64]bool) |
| 50 | for id := range ch { |
| 51 | if _, ok := mp[id]; ok { |
| 52 | t.Error("It should not be repeated") |
| 53 | break |
| 54 | } |
| 55 | mp[id] = true |
| 56 | } |
| 57 | if len(mp) != le { |
| 58 | t.Error("map length should be equal", le) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestSetStartTime(t *testing.T) { |
| 63 | t.Run("A nil time", func(tt *testing.T) { |
| 64 | defer func() { |
| 65 | if e := recover(); e == nil { |
| 66 | tt.Error("Should throw a error when start time is zero") |
| 67 | } else if e.(string) != "The start time cannot be a zero value" { |
| 68 | tt.Error("The error message should equal [The start time cannot be a zero value]") |
| 69 | } |
| 70 | }() |
| 71 | var time time.Time |
| 72 | snowflake.SetStartTime(time) |
| 73 | }) |
| 74 | |
| 75 | t.Run("Start time too big", func(tt *testing.T) { |
| 76 | defer func() { |
| 77 | if e := recover(); e == nil { |
| 78 | tt.Error("Should throw a error when start time is too big") |
| 79 | } else if e.(string) != "The s cannot be greater than the current millisecond" { |
| 80 | tt.Error("The error message should equal [The s cannot be greater than the current millisecond]") |
| 81 | } |
| 82 | }() |
| 83 | time := time.Date(2035, 1, 1, 1, 0, 0, 0, time.UTC) |
| 84 | snowflake.SetStartTime(time) |
| 85 | }) |
| 86 | |
| 87 | t.Run("Start time too small", func(tt *testing.T) { |
| 88 | defer func() { |
| 89 | if e := recover(); e == nil { |
| 90 | tt.Error("Should throw a error when starttime is too small") |
| 91 | } else if e.(string) != "The maximum life cycle of the snowflake algorithm is 69 years" { |
| 92 | tt.Error("The error message should equal [The maximum life cycle of the snowflake algorithm is 69 years]") |
| 93 | } |
| 94 | }() |
| 95 | // because 2021-69 = 1952, set df time > 69 years to test. |
| 96 | time := time.Date(1951, 1, 1, 1, 0, 0, 0, time.UTC) |
| 97 | snowflake.SetStartTime(time) |
| 98 | }) |
| 99 | |
| 100 | t.Run("Default start time", func(tt *testing.T) { |
| 101 | defaultTime := time.Date(2008, 11, 10, 23, 0, 0, 0, time.UTC) |
| 102 | defaultNano := defaultTime.UTC().UnixNano() / 1e6 |
| 103 | |
| 104 | sid := snowflake.ParseID(snowflake.ID()) |
| 105 | currentTime := sid.Timestamp + uint64(defaultNano) |
| 106 | |
| 107 | nowNano := time.Now().UTC().UnixNano() / 1e6 |
| 108 | |
| 109 | // approximate equality, Assuming that the program is completed in one second. |
| 110 | if currentTime/1000 != uint64(nowNano)/1000 { |
| 111 | t.Error("The timestamp should be equal") |
| 112 | } |
| 113 | }) |
| 114 | |
| 115 | t.Run("Basic", func(tt *testing.T) { |
| 116 | date := time.Date(2002, 1, 1, 1, 0, 0, 0, time.UTC) |
| 117 | snowflake.SetStartTime(date) |
| 118 | |
| 119 | nowNano := time.Now().UTC().UnixNano() / 1e6 |
| 120 | startNano := date.UTC().UnixNano() / 1e6 |
| 121 | df := nowNano - startNano |
| 122 | |
| 123 | sid := snowflake.ParseID(snowflake.ID()) |
| 124 | |
| 125 | // approximate equality, Assuming that the program is completed in one second. |
| 126 | if sid.Timestamp/1000 != uint64(df)/1000 { |
| 127 | t.Error("The timestamp should be equal") |
| 128 | } |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | func TestSetMachineID(t *testing.T) { |
| 133 | // first test, |
| 134 | sid := snowflake.ParseID(snowflake.ID()) |
| 135 | if sid.MachineID != 0 { |
| 136 | t.Error("MachineID should be equal 0") |
| 137 | } |
| 138 | |
| 139 | t.Run("No Panic", func(tt *testing.T) { |
| 140 | defer func() { |
| 141 | if err := recover(); err != nil { |
| 142 | t.Error("An error should not be returned") |
| 143 | } |
| 144 | }() |
| 145 | |
| 146 | snowflake.SetMachineID(1) |
| 147 | id := snowflake.ID() |
| 148 | sid := snowflake.ParseID(id) |
| 149 | |
| 150 | if sid.MachineID != 1 { |
| 151 | tt.Error("The machineID should be equal 1") |
| 152 | } |
| 153 | }) |
| 154 | |
| 155 | t.Run("Panic", func(tt *testing.T) { |
| 156 | defer func() { |
| 157 | if err := recover(); err == nil { |
| 158 | tt.Error("Should throw a error") |
| 159 | } else if err.(string) != "The machineid cannot be greater than 1023" { |
| 160 | tt.Error("The error message should be eq 「The machineid cannot be greater than 1023」") |
| 161 | } |
| 162 | }() |
| 163 | |
| 164 | snowflake.SetMachineID(1024) |
| 165 | }) |
| 166 | |
| 167 | snowflake.SetMachineID(100) |
| 168 | sid = snowflake.ParseID(snowflake.ID()) |
| 169 | if sid.MachineID != 100 { |
| 170 | t.Error("MachineID should be equal 100") |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestSetSequenceResolver(t *testing.T) { |
| 175 | snowflake.SetSequenceResolver(func(c int64) (uint16, error) { |
| 176 | return 100, nil |
| 177 | }) |
| 178 | |
| 179 | id := snowflake.ID() |
| 180 | sid := snowflake.ParseID(id) |
| 181 | |
| 182 | if sid.Sequence != 100 { |
| 183 | t.Error("The snowflake number part of sequence should be equal 100") |
| 184 | } |
| 185 | |
| 186 | time.Sleep(time.Millisecond) |
| 187 | |
| 188 | id = snowflake.ID() |
| 189 | sid2 := snowflake.ParseID(id) |
| 190 | |
| 191 | if sid2.Sequence != 100 { |
| 192 | t.Error("The snowflake number part of sequence should be equal 100") |
| 193 | } |
| 194 | |
| 195 | if sid2.Timestamp <= sid.Timestamp { |
| 196 | t.Error("It should be bigger than the previous time") |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func TestNextID(t *testing.T) { |
| 201 | _, err := snowflake.NextID() |
| 202 | if err != nil { |
| 203 | t.Error(err) |
| 204 | return |
| 205 | } |
| 206 | |
| 207 | snowflake.SetSequenceResolver(func(ms int64) (uint16, error) { |
| 208 | return 0, errors.New("test error") |
| 209 | }) |
| 210 | _, e := snowflake.NextID() |
| 211 | if e == nil { |
| 212 | t.Error("Should be throw error") |
| 213 | } else if e.Error() != "test error" { |
| 214 | t.Error("NextID error message should be equal [test error]") |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func TestParseID(t *testing.T) { |
| 219 | time := 101 << (snowflake.MachineIDLength + snowflake.SequenceLength) |
| 220 | machineid := 1023 << snowflake.SequenceLength |
| 221 | seq := 999 |
| 222 | |
| 223 | id := uint64(time | machineid | seq) |
| 224 | |
| 225 | d := snowflake.ParseID(id) |
| 226 | if d.Sequence != 999 { |
| 227 | t.Error("Sequence should be equal 999") |
| 228 | } |
| 229 | |
| 230 | if d.MachineID != 1023 { |
| 231 | t.Error("MachineID should be equal 1023") |
| 232 | } |
| 233 | |
| 234 | if d.Timestamp != 101 { |
| 235 | t.Error("Timestamp should be equal 101") |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | func TestSID_GenerateTime(t *testing.T) { |
| 240 | snowflake.SetSequenceResolver(snowflake.AtomicResolver) |
| 241 | a, e := snowflake.NextID() |
| 242 | if e != nil { |
| 243 | t.Error(e) |
| 244 | return |
| 245 | } |
| 246 | |
| 247 | sid := snowflake.ParseID(a) |
| 248 | |
| 249 | if sid.GenerateTime().UTC().Second() != time.Now().UTC().Second() { |
| 250 | t.Error("The id generate time should be equal current time") |
| 251 | } |
| 252 | } |