2
2
BlockEntity ,
3
3
PgConnectionConfig ,
4
4
STAKE_POOL_METADATA_QUEUE ,
5
+ createDataSource ,
5
6
createPgBossExtension
6
7
} from '@cardano-sdk/projection-typeorm' ;
7
8
import { Cardano } from '@cardano-sdk/core' ;
@@ -11,7 +12,7 @@ import { PgBossHttpService, pgBossEntities } from '../../../src/Program/services
11
12
import { Pool } from 'pg' ;
12
13
import { StakePoolMetadataFetchMode } from '../../../src/Program/options' ;
13
14
import { WorkerHandlerFactoryOptions } from '../../../src/PgBoss' ;
14
- import { createObservableDataSource , getConnectionConfig , getPool } from '../../../src' ;
15
+ import { getConnectionConfig , getPool } from '../../../src/Program/services/postgres ' ;
15
16
import { logger } from '@cardano-sdk/util-dev' ;
16
17
17
18
const dnsResolver = ( ) => Promise . resolve ( { name : 'localhost' , port : 5433 , priority : 6 , weight : 5 } ) ;
@@ -70,6 +71,7 @@ jest.mock('@cardano-sdk/projection-typeorm', () => {
70
71
71
72
describe ( 'PgBossHttpService' , ( ) => {
72
73
let connectionConfig$ : Observable < PgConnectionConfig > ;
74
+ let connectionConfig : PgConnectionConfig ;
73
75
let dataSource : DataSource ;
74
76
let db : Pool ;
75
77
let service : PgBossHttpService | undefined ;
@@ -87,15 +89,7 @@ describe('PgBossHttpService', () => {
87
89
} ;
88
90
89
91
connectionConfig$ = getConnectionConfig ( dnsResolver , 'test' , 'StakePool' , args ) ;
90
- const dataSource$ = createObservableDataSource ( {
91
- connectionConfig$,
92
- devOptions : { dropSchema : true , synchronize : true } ,
93
- entities : pgBossEntities ,
94
- extensions : { pgBoss : true } ,
95
- logger,
96
- migrationsRun : false
97
- } ) ;
98
- dataSource = await firstValueFrom ( dataSource$ ) ;
92
+ connectionConfig = await firstValueFrom ( connectionConfig$ ) ;
99
93
100
94
const pool = await getPool ( dnsResolver , logger , args ) ;
101
95
@@ -104,8 +98,20 @@ describe('PgBossHttpService', () => {
104
98
db = pool ;
105
99
} ) ;
106
100
101
+ beforeEach ( async ( ) => {
102
+ dataSource = createDataSource ( {
103
+ connectionConfig,
104
+ devOptions : { dropSchema : true , synchronize : true } ,
105
+ entities : pgBossEntities ,
106
+ extensions : { pgBoss : true } ,
107
+ logger
108
+ } ) ;
109
+ await dataSource . initialize ( ) ;
110
+ } ) ;
111
+
107
112
afterEach ( async ( ) => {
108
113
await service ?. shutdown ( ) ;
114
+ await dataSource . destroy ( ) . catch ( ( ) => void 0 ) ;
109
115
} ) ;
110
116
111
117
it ( 'health check is ok after start with a valid db connection' , async ( ) => {
@@ -116,7 +122,6 @@ describe('PgBossHttpService', () => {
116
122
parallelJobs : 3 ,
117
123
queues : [ ]
118
124
} ,
119
-
120
125
{ connectionConfig$, db, logger }
121
126
) ;
122
127
expect ( await service . healthCheck ( ) ) . toEqual ( { ok : false , reason : 'PgBossHttpService not started' } ) ;
@@ -132,7 +137,6 @@ describe('PgBossHttpService', () => {
132
137
let observableResolver = ( ) => { } ;
133
138
let subscriptions = 0 ;
134
139
135
- const connectionConfig = await firstValueFrom ( connectionConfig$ ) ;
136
140
const config$ = new Observable < PgConnectionConfig > ( ( subscriber ) => {
137
141
subscriptions ++ ;
138
142
@@ -155,6 +159,8 @@ describe('PgBossHttpService', () => {
155
159
await service . start ( ) ;
156
160
157
161
// Insert test block with slot 1
162
+ const queryRunner = dataSource . createQueryRunner ( ) ;
163
+ await queryRunner . connect ( ) ;
158
164
const blockRepos = dataSource . getRepository ( BlockEntity ) ;
159
165
const block = { hash : 'test' , height : 1 , slot : 1 } ;
160
166
await blockRepos . insert ( block ) ;
@@ -165,7 +171,6 @@ describe('PgBossHttpService', () => {
165
171
expect ( await collectStatus ( ) ) . toEqual ( { calls : 0 , health : { ok : true } , subscriptions : 1 } ) ;
166
172
167
173
// Schedule a job
168
- const queryRunner = dataSource . createQueryRunner ( ) ;
169
174
const pgboss = createPgBossExtension ( queryRunner , logger ) ;
170
175
await pgboss . send ( STAKE_POOL_METADATA_QUEUE , { } , { retryDelay : 1 , retryLimit : 100 , slot : Cardano . Slot ( 1 ) } ) ;
171
176
await queryRunner . release ( ) ;
@@ -225,5 +230,7 @@ describe('PgBossHttpService', () => {
225
230
await testPromises [ 3 ] ;
226
231
227
232
expect ( await collectStatus ( ) ) . toEqual ( { calls : 4 , health : { ok : true } , subscriptions : 3 } ) ;
233
+
234
+ await dataSource . destroy ( ) ;
228
235
} ) ;
229
236
} ) ;
0 commit comments