@@ -10,65 +10,39 @@ const client = new DynamoDB({
10
10
endpoint : 'http://localhost:8000'
11
11
} ) ;
12
12
13
- const putItems = async ( ) => {
14
- // Dynamodb-local doesn't create stream until table isn't empty
15
- await Promise . all ( [
16
- client
17
- . putItem ( {
18
- Item : { id : { S : `Bug` } } ,
19
- TableName : 'MyFirstTable'
20
- } )
21
- . promise ( ) ,
22
- client
23
- . putItem ( {
24
- Item : { id : { S : `Bug` } } ,
25
- TableName : 'MySecondTable'
26
- } )
27
- . promise ( ) ,
28
- client
29
- . putItem ( {
30
- Item : { id : { S : `Bug` } } ,
31
- TableName : 'MyThirdTable'
32
- } )
33
- . promise ( ) ,
34
- client
35
- . putItem ( {
36
- Item : { id : { S : `Bug` } } ,
37
- TableName : 'MyFourthTable'
38
- } )
39
- . promise ( )
40
- ] ) ;
41
- // wait stream get a new iterator
13
+ // Dynamodb-local doesn't create stream until table isn't empty
14
+ const unemptyTables = ( ) =>
15
+ Promise . all (
16
+ [ 'MyFirstTable' , 'MySecondTable' , 'MyThirdTable' , 'MyFourthTable' ] . map ( TableName =>
17
+ client
18
+ . putItem ( {
19
+ Item : { id : { S : 'Stub' } } ,
20
+ TableName
21
+ } )
22
+ . promise ( )
23
+ )
24
+ ) ;
25
+
26
+ const putItems = ( ) =>
27
+ Promise . all (
28
+ [ 'First' , 'Second' , 'Third' , 'Fourth' ] . map ( order =>
29
+ client
30
+ . putItem ( {
31
+ Item : { id : { S : `My${ order } Id` } } ,
32
+ TableName : `My${ order } Table`
33
+ } )
34
+ . promise ( )
35
+ )
36
+ ) ;
37
+
38
+ let setupInProgress = true ;
39
+ const populateTables = async ( ) => {
40
+ await unemptyTables ( ) ;
42
41
await new Promise ( resolve => {
43
- setTimeout ( resolve , 1000 ) ;
42
+ setTimeout ( resolve , 1200 ) ;
44
43
} ) ;
45
-
46
- await Promise . all ( [
47
- client
48
- . putItem ( {
49
- Item : { id : { S : `MyFirstId` } } ,
50
- TableName : 'MyFirstTable'
51
- } )
52
- . promise ( ) ,
53
- client
54
- . putItem ( {
55
- Item : { id : { S : `MySecondId` } } ,
56
- TableName : 'MySecondTable'
57
- } )
58
- . promise ( ) ,
59
- client
60
- . putItem ( {
61
- Item : { id : { S : `MyThirdId` } } ,
62
- TableName : 'MyThirdTable'
63
- } )
64
- . promise ( ) ,
65
- client
66
- . putItem ( {
67
- Item : { id : { S : `MyFourthId` } } ,
68
- TableName : 'MyFourthTable'
69
- } )
70
- . promise ( )
71
- ] ) ;
44
+ setupInProgress = false ;
45
+ await putItems ( ) ;
72
46
} ;
73
47
74
48
const serverless = spawn (
@@ -81,43 +55,28 @@ const serverless = spawn(
81
55
) ;
82
56
83
57
const set = new Set ( ) ;
58
+ let invocationCount = 0 ;
84
59
serverless . stdout . pipe (
85
60
new Writable ( {
86
61
write ( chunk , enc , cb ) {
87
62
const output = chunk . toString ( ) ;
88
63
89
64
if ( / S t a r t i n g O f f l i n e D y n a m o d b S t r e a m s / . test ( output ) ) {
90
- putItems ( ) ;
65
+ populateTables ( ) ; // will run in the background
91
66
}
92
67
68
+ if ( setupInProgress ) return cb ( ) ; // do not consider lambda executions before we post the real items
69
+
93
70
const matches = / o f f l i n e : \( λ : ( .* ) \) R e q u e s t I d : .* D u r a t i o n : .* m s { 2 } B i l l e d D u r a t i o n : .* m s / g. exec (
94
71
output
95
72
) ;
96
73
97
- if ( matches ) set . add ( matches [ 1 ] ) ;
98
-
99
- if ( set . size === 4 ) serverless . kill ( ) ;
100
- cb ( ) ;
101
- }
102
- } )
103
- ) ;
104
- serverless . stdout . pipe (
105
- new Writable ( {
106
- write ( chunk , enc , cb ) {
107
- const output = chunk . toString ( ) ;
108
-
109
- if ( / S t a r t i n g O f f l i n e D y n a m o d b S t r e a m s / . test ( output ) ) {
110
- putItems ( ) ;
74
+ if ( matches ) {
75
+ invocationCount ++ ;
76
+ set . add ( matches [ 1 ] ) ;
111
77
}
112
78
113
- this . count =
114
- ( this . count || 0 ) +
115
- (
116
- output . match (
117
- / o f f l i n e : \( λ : .* \) R e q u e s t I d : .* D u r a t i o n : .* m s { 2 } B i l l e d D u r a t i o n : .* m s / g
118
- ) || [ ]
119
- ) . length ;
120
- if ( this . count === 4 ) serverless . kill ( ) ;
79
+ if ( set . size === 3 && invocationCount === 4 ) serverless . kill ( ) ; // myPromiseHandler is mapped to two tables
121
80
cb ( ) ;
122
81
}
123
82
} )
0 commit comments