1
- import { StepFunctionContextService } from "./step-function-service" ;
1
+ import { PARENT_ID , StepFunctionContextService } from "./step-function-service" ;
2
2
3
3
describe ( "StepFunctionContextService" , ( ) => {
4
4
const stepFunctionEvent = {
@@ -194,8 +194,8 @@ describe("StepFunctionContextService", () => {
194
194
195
195
expect ( spanContext ) . not . toBeNull ( ) ;
196
196
197
- expect ( spanContext ?. toTraceId ( ) ) . toBe ( "947965466153612645 " ) ;
198
- expect ( spanContext ?. toSpanId ( ) ) . toBe ( "4602916161841036335 " ) ;
197
+ expect ( spanContext ?. toTraceId ( ) ) . toBe ( "1139193989631387307 " ) ;
198
+ expect ( spanContext ?. toSpanId ( ) ) . toBe ( "5892738536804826142 " ) ;
199
199
expect ( spanContext ?. sampleMode ( ) ) . toBe ( "1" ) ;
200
200
expect ( spanContext ?. source ) . toBe ( "event" ) ;
201
201
} ) ;
@@ -211,54 +211,55 @@ describe("StepFunctionContextService", () => {
211
211
} ) ;
212
212
} ) ;
213
213
214
- describe ( "deterministicMd5HashToBigIntString " , ( ) => {
214
+ describe ( "deterministicSha256HashToBigIntString " , ( ) => {
215
215
it ( "returns the same hash number generated in `logs backend` for a random string" , ( ) => {
216
216
const instance = StepFunctionContextService . instance ( ) ;
217
- const hash = instance [ "deterministicMd5HashToBigIntString " ] ( "some_testing_random_string" ) ;
218
- expect ( hash ) . toEqual ( "2251275791555400689 " ) ;
217
+ const hash = instance [ "deterministicSha256HashToBigIntString " ] ( "some_testing_random_string" , PARENT_ID ) ;
218
+ expect ( hash ) . toEqual ( "4364271812988819936 " ) ;
219
219
} ) ;
220
220
221
221
it ( "returns the same hash number generated in `logs backend` for execution id # state name # entered time" , ( ) => {
222
222
const instance = StepFunctionContextService . instance ( ) ;
223
- const hash = instance [ "deterministicMd5HashToBigIntString " ] (
223
+ const hash = instance [ "deterministicSha256HashToBigIntString " ] (
224
224
"arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111#step-one#2022-12-08T21:08:19.224Z" ,
225
+ PARENT_ID ,
225
226
) ;
226
- expect ( hash ) . toEqual ( "8034507082463708833 " ) ;
227
+ expect ( hash ) . toEqual ( "4340734536022949921 " ) ;
227
228
} ) ;
228
229
} ) ;
229
230
230
- describe ( "deterministicMd5HashInBinary " , ( ) => {
231
+ describe ( "deterministicSha256Hash " , ( ) => {
231
232
it . each ( [
232
233
[
233
234
"a random string" ,
234
235
"some_testing_random_string" ,
235
- "0001111100111110001000110110011110010111000110001001001111110001 " ,
236
+ "0011110010010001000000100001011101001100011100101101100111100000 " ,
236
237
] ,
237
238
[
238
239
"an execution id" ,
239
240
"arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d041f4" ,
240
- "0010010000101100100000101011111101111100110110001110111100111101 " ,
241
+ "0100010100110010010010100001011001110100111011010100110010000100 " ,
241
242
] ,
242
243
[
243
244
"another execution id" ,
244
245
"arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111" ,
245
- "0010001100110000011011011111010000100111100000110000100100101010 " ,
246
+ "0010111110001100100010000101001100110000000000010111011100101011 " ,
246
247
] ,
247
248
[
248
249
"execution id # state name # entered time" ,
249
250
"arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111#step-one#2022-12-08T21:08:19.224Z" ,
250
- "0110111110000000010011011001111101110011100111000000011010100001 " ,
251
+ "0011110000111101011000110000111111110011111010110000000000100001 " ,
251
252
] ,
252
253
] ) ( "returns the same hash number generated in `logs backend` for %s" , ( _ , str , expected ) => {
253
254
const instance = StepFunctionContextService . instance ( ) ;
254
- const hash = instance [ "deterministicMd5HashInBinary " ] ( str ) ;
255
+ const hash = instance [ "deterministicSha256Hash " ] ( str , PARENT_ID ) ;
255
256
expect ( hash ) . toEqual ( expected ) ;
256
257
} ) ;
257
258
258
259
it ( "returns a hash always leading with 0" , ( ) => {
259
260
const instance = StepFunctionContextService . instance ( ) ;
260
261
for ( let i = 0 ; i < 20 ; i ++ ) {
261
- const hash = instance [ "deterministicMd5HashInBinary " ] ( i . toString ( ) ) ;
262
+ const hash = instance [ "deterministicSha256Hash " ] ( i . toString ( ) , PARENT_ID ) ;
262
263
expect ( hash . substring ( 0 , 1 ) ) . toMatch ( "0" ) ;
263
264
}
264
265
} ) ;
@@ -268,36 +269,44 @@ describe("StepFunctionContextService", () => {
268
269
const times = 20 ;
269
270
for ( let i = 0 ; i < times ; i ++ ) {
270
271
for ( let j = i + 1 ; j < times ; j ++ ) {
271
- const hash1 = instance [ "deterministicMd5HashInBinary " ] ( i . toString ( ) ) ;
272
- const hash2 = instance [ "deterministicMd5HashInBinary " ] ( j . toString ( ) ) ;
272
+ const hash1 = instance [ "deterministicSha256Hash " ] ( i . toString ( ) , PARENT_ID ) ;
273
+ const hash2 = instance [ "deterministicSha256Hash " ] ( j . toString ( ) , PARENT_ID ) ;
273
274
expect ( hash1 ) . not . toMatch ( hash2 ) ;
274
275
}
275
276
}
276
277
} ) ;
277
278
} ) ;
278
279
279
- describe ( "hexToBinary " , ( ) => {
280
+ describe ( "numberToBinaryString " , ( ) => {
280
281
const instance = StepFunctionContextService . instance ( ) ;
281
282
it . each ( [
282
- [ "0" , "0000" ] ,
283
- [ "1" , "0001" ] ,
284
- [ "2" , "0010" ] ,
285
- [ "3" , "0011" ] ,
286
- [ "4" , "0100" ] ,
287
- [ "5" , "0101" ] ,
288
- [ "6" , "0110" ] ,
289
- [ "7" , "0111" ] ,
290
- [ "8" , "1000" ] ,
291
- [ "9" , "1001" ] ,
292
- [ "a" , "1010" ] ,
293
- [ "b" , "1011" ] ,
294
- [ "c" , "1100" ] ,
295
- [ "d" , "1101" ] ,
296
- [ "e" , "1110" ] ,
297
- [ "f" , "1111" ] ,
283
+ [ 0 , "00000000" ] ,
284
+ [ 1 , "00000001" ] ,
285
+ [ 2 , "00000010" ] ,
286
+ [ 3 , "00000011" ] ,
287
+ [ 4 , "00000100" ] ,
298
288
] ) ( "returns the right binary number for %s => %s" , ( hex , expected ) => {
299
- const binary = instance [ "hexToBinary " ] ( hex ) ;
289
+ const binary = instance [ "numberToBinaryString " ] ( hex ) ;
300
290
expect ( binary ) . toBe ( expected ) ;
301
291
} ) ;
302
292
} ) ;
293
+
294
+ describe ( "test 64 bits deterministicSha256HashToBigIntString for span id" , ( ) => {
295
+ const instance = StepFunctionContextService . instance ( ) ;
296
+ it ( "first test of #1" , ( ) => {
297
+ const actual = instance [ "deterministicSha256HashToBigIntString" ] (
298
+ "arn:aws:states:sa-east-1:425362996713:stateMachine:MyStateMachine-b276uka1j#lambda#1" ,
299
+ PARENT_ID ,
300
+ ) ;
301
+ expect ( actual ) . toEqual ( "3711631873188331089" ) ;
302
+ } ) ;
303
+
304
+ it ( "test same hashing number is generated as logs-backend for execution id # state name # entered time" , ( ) => {
305
+ const actual = instance [ "deterministicSha256HashToBigIntString" ] (
306
+ "arn:aws:states:sa-east-1:425362996713:stateMachine:MyStateMachine-b276uka1j#lambda#2" ,
307
+ PARENT_ID ,
308
+ ) ;
309
+ expect ( actual ) . toEqual ( "5759173372325510050" ) ;
310
+ } ) ;
311
+ } ) ;
303
312
} ) ;
0 commit comments