@@ -11,45 +11,23 @@ See the License for the specific language governing permissions and
1111limitations under the License.
1212*/
1313
14- // import { setTimeout } from "node:timers/promises";
15- // import { GenericContainer, Network, Wait } from "testcontainers";
1614import path from "node:path" ;
17- import { Network } from "testcontainers" ;
15+ import { Network , TestContainers } from "testcontainers" ;
16+ import { DaprClient , DaprServer , LogLevel } from "@dapr/dapr" ;
1817import { DAPR_RUNTIME_IMAGE , DaprContainer } from "./DaprContainer" ;
1918
20- // jest.setTimeout(120_000);
21-
2219describe ( "DaprContainer" , ( ) => {
23- it ( "should start and stop" , async ( ) => {
24- const network = await new Network ( ) . start ( ) ;
25- const container = new DaprContainer ( "daprio/dapr:latest" )
26- . withNetwork ( network )
27- . withAppName ( "dapr-app" )
28- . withAppPort ( 8081 )
29- . withDaprLogLevel ( "debug" )
30- . withAppChannelAddress ( "host.testcontainers.internal" ) ;
31- const startedContainer = await container . start ( ) ;
32- expect ( startedContainer . getHost ( ) ) . toBeDefined ( ) ;
33- expect ( startedContainer . getHttpPort ( ) ) . toBeDefined ( ) ;
34- expect ( startedContainer . getGrpcPort ( ) ) . toBeDefined ( ) ;
35- expect ( startedContainer . getHttpEndpoint ( ) ) . toBeDefined ( ) ;
36- expect ( startedContainer . getGrpcEndpoint ( ) ) . toBeDefined ( ) ;
37- await startedContainer . stop ( ) ;
38- await network . stop ( ) ;
39- } , 120_000 ) ;
4020 it ( "should load component from path" , async ( ) => {
4121 const componentPath = path . join ( __dirname , "__fixtures__" , "dapr-resources" , "statestore.yaml" ) ;
4222 const dapr = new DaprContainer ( DAPR_RUNTIME_IMAGE )
4323 . withAppName ( "dapr-app" )
4424 . withAppPort ( 8081 )
4525 . withComponentFromPath ( componentPath )
4626 . withAppChannelAddress ( "host.testcontainers.internal" ) ;
47-
4827 const components = dapr . getComponents ( ) ;
4928 expect ( components . length ) . toBe ( 1 ) ;
5029 const kvstore = components [ 0 ] ;
5130 expect ( kvstore . getMetadata ( ) . length ) . toBeTruthy ( ) ;
52-
5331 const componentYaml = kvstore . toYaml ( ) ;
5432 const expectedComponentYaml =
5533 "apiVersion: dapr.io/v1alpha1\n" +
@@ -66,7 +44,124 @@ describe("DaprContainer", () => {
6644 " value: redis:6379\n" +
6745 " - name: redisPassword\n" +
6846 ' value: ""\n' ;
69-
7047 expect ( componentYaml ) . toBe ( expectedComponentYaml ) ;
7148 } ) ;
49+
50+ it ( "should start and stop" , async ( ) => {
51+ const network = await new Network ( ) . start ( ) ;
52+ const dapr = new DaprContainer ( DAPR_RUNTIME_IMAGE )
53+ . withNetwork ( network )
54+ . withDaprLogLevel ( "debug" )
55+ . withAppChannelAddress ( "host.testcontainers.internal" ) ;
56+ const startedContainer = await dapr . start ( ) ;
57+ expect ( startedContainer . getHost ( ) ) . toBeDefined ( ) ;
58+ expect ( startedContainer . getHttpPort ( ) ) . toBeDefined ( ) ;
59+ expect ( startedContainer . getGrpcPort ( ) ) . toBeDefined ( ) ;
60+ expect ( startedContainer . getHttpEndpoint ( ) ) . toBeDefined ( ) ;
61+ expect ( startedContainer . getGrpcEndpoint ( ) ) . toBeDefined ( ) ;
62+ await startedContainer . stop ( ) ;
63+ await network . stop ( ) ;
64+ } , 60_000 ) ;
65+
66+ it ( "should initialize DaprClient" , async ( ) => {
67+ const network = await new Network ( ) . start ( ) ;
68+ const dapr = new DaprContainer ( DAPR_RUNTIME_IMAGE )
69+ . withNetwork ( network )
70+ . withDaprLogLevel ( "debug" )
71+ . withAppChannelAddress ( "host.testcontainers.internal" ) ;
72+ const startedContainer = await dapr . start ( ) ;
73+
74+ const client = new DaprClient ( {
75+ daprHost : startedContainer . getHost ( ) ,
76+ daprPort : startedContainer . getHttpPort ( ) . toString ( ) ,
77+ } ) ;
78+ await client . start ( ) ;
79+ expect ( client . getIsInitialized ( ) ) . toBe ( true ) ;
80+ await client . stop ( ) ;
81+
82+ await startedContainer . stop ( ) ;
83+ await network . stop ( ) ;
84+ } , 60_000 ) ;
85+
86+ it ( "should provide kvstore in memory by default" , async ( ) => {
87+ const network = await new Network ( ) . start ( ) ;
88+ const dapr = new DaprContainer ( DAPR_RUNTIME_IMAGE )
89+ . withNetwork ( network )
90+ . withDaprLogLevel ( "debug" )
91+ . withAppChannelAddress ( "host.testcontainers.internal" )
92+ const startedContainer = await dapr . start ( ) ;
93+
94+ const client = new DaprClient ( {
95+ daprHost : startedContainer . getHost ( ) ,
96+ daprPort : startedContainer . getHttpPort ( ) . toString ( ) ,
97+ } ) ;
98+ await client . start ( ) ;
99+ expect ( client . getIsInitialized ( ) ) . toBe ( true ) ;
100+ await client . state . save ( "kvstore" , [ { key : "key" , value : "value" } ] ) ;
101+ const result = await client . state . get ( "kvstore" , "key" ) ;
102+ expect ( result ) . toEqual ( "value" ) ;
103+ await client . state . delete ( "kvstore" , "key" ) ;
104+ const resultAfterDelete = await client . state . get ( "kvstore" , "key" ) ;
105+ expect ( resultAfterDelete ) . toBe ( "" ) ;
106+ await client . stop ( ) ;
107+
108+ await startedContainer . stop ( ) ;
109+ await network . stop ( ) ;
110+ } , 60_000 ) ;
111+
112+ it ( "should provide pubsub in memory by default" , async ( ) => {
113+ TestContainers . exposeHostPorts ( 8081 ) ;
114+
115+ const network = await new Network ( ) . start ( ) ;
116+ const dapr = new DaprContainer ( DAPR_RUNTIME_IMAGE )
117+ . withNetwork ( network )
118+ . withAppPort ( 8081 )
119+ . withDaprLogLevel ( "debug" )
120+ . withAppChannelAddress ( "host.testcontainers.internal" )
121+ const startedContainer = await dapr . start ( ) ;
122+
123+ const server = new DaprServer ( {
124+ serverHost : "127.0.0.1" ,
125+ serverPort : "8081" ,
126+ clientOptions : {
127+ daprHost : startedContainer . getHost ( ) ,
128+ daprPort : startedContainer . getHttpPort ( ) . toString ( )
129+ } ,
130+ logger : { level : LogLevel . Debug } ,
131+ } ) ;
132+
133+ const client = new DaprClient ( {
134+ daprHost : startedContainer . getHost ( ) ,
135+ daprPort : startedContainer . getHttpPort ( ) . toString ( ) ,
136+ logger : { level : LogLevel . Debug } ,
137+ } ) ;
138+
139+ // Promise to resolve when the message is received
140+ let processMessage : ( data ?: unknown ) => void ;
141+ const promise = new Promise ( ( res ) => {
142+ processMessage = res ;
143+ } ) ;
144+
145+ await server . pubsub . subscribe ( "pubsub" , "topic" , async ( message ) => {
146+ console . log ( "Message received:" , message ) ;
147+ processMessage ( message ) ;
148+ } ) ;
149+
150+ await server . start ( ) ;
151+ // Wait for the server to start
152+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
153+
154+ console . log ( "Publishing message..." ) ;
155+ const response = await client . pubsub . publish ( "pubsub" , "topic" , { key : "key" , value : "value" } ) ;
156+ console . log ( "Publish response:" , response ) ;
157+
158+ // Wait for the message to be processed
159+ // await new Promise((resolve) => setTimeout(resolve, 5000));
160+ const result = await promise ; // FIXME
161+ expect ( result ) . toEqual ( { key : "key" , value : "value" } ) ;
162+
163+ await server . stop ( ) ;
164+ await startedContainer . stop ( ) ;
165+ await network . stop ( ) ;
166+ } , 120_000 ) ;
72167} ) ;
0 commit comments