@@ -2,11 +2,9 @@ import crypto from 'crypto';
22import { nextTick } from 'process' ;
33import { promisify } from 'util' ;
44import { AttachmentBacktraceDatabaseRecord , BacktraceData , ReportBacktraceDatabaseRecord } from '../../src/index.js' ;
5- import { RequestBacktraceReportSubmission } from '../../src/model/http/BacktraceReportSubmission.js' ;
65import { BacktraceDatabase } from '../../src/modules/database/BacktraceDatabase.js' ;
76import { BacktraceDatabaseContext } from '../../src/modules/database/BacktraceDatabaseContext.js' ;
8- import { TEST_SUBMISSION_URL } from '../mocks/BacktraceTestClient.js' ;
9- import { testHttpClient } from '../mocks/testHttpClient.js' ;
7+ import { BacktraceTestClient } from '../mocks/BacktraceTestClient.js' ;
108import { getTestStorageProvider } from '../mocks/testStorageProvider.js' ;
119
1210function randomReportRecord ( ) : ReportBacktraceDatabaseRecord {
@@ -35,16 +33,7 @@ function randomAttachmentRecord(): AttachmentBacktraceDatabaseRecord {
3533describe ( 'Database setup tests' , ( ) => {
3634 it ( 'The database should be disabled by default' , ( ) => {
3735 const testStorageProvider = getTestStorageProvider ( ) ;
38- const database = new BacktraceDatabase (
39- undefined ,
40- testStorageProvider ,
41- new RequestBacktraceReportSubmission (
42- {
43- url : TEST_SUBMISSION_URL ,
44- } ,
45- testHttpClient ,
46- ) ,
47- ) ;
36+ const database = new BacktraceDatabase ( undefined , testStorageProvider ) ;
4837
4938 expect ( database . enabled ) . toBeFalsy ( ) ;
5039 } ) ;
@@ -56,37 +45,30 @@ describe('Database setup tests', () => {
5645 autoSend : false ,
5746 } ,
5847 testStorageProvider ,
59- new RequestBacktraceReportSubmission (
60- {
61- url : TEST_SUBMISSION_URL ,
62- } ,
63- testHttpClient ,
64- ) ,
6548 ) ;
66-
67- const databaseStartResult = database . initialize ( ) ;
68-
69- expect ( databaseStartResult ) . toBeTruthy ( ) ;
70- expect ( database . enabled ) . toBeTruthy ( ) ;
49+ const client = BacktraceTestClient . buildFakeClient ( undefined , undefined , undefined , undefined , [ database ] ) ;
50+ try {
51+ const databaseStartResult = database . initialize ( ) ;
52+
53+ expect ( databaseStartResult ) . toBeTruthy ( ) ;
54+ expect ( database . enabled ) . toBeTruthy ( ) ;
55+ } finally {
56+ client . dispose ( ) ;
57+ }
7158 } ) ;
7259
7360 it ( 'Should not enable the database if the enable option is set to false' , ( ) => {
7461 const testStorageProvider = getTestStorageProvider ( ) ;
75- const database = new BacktraceDatabase (
76- { enable : false } ,
77- testStorageProvider ,
78- new RequestBacktraceReportSubmission (
79- {
80- url : TEST_SUBMISSION_URL ,
81- } ,
82- testHttpClient ,
83- ) ,
84- ) ;
85-
86- const databaseStartResult = database . initialize ( ) ;
87-
88- expect ( databaseStartResult ) . toBeFalsy ( ) ;
89- expect ( database . enabled ) . toBeFalsy ( ) ;
62+ const database = new BacktraceDatabase ( { enable : false } , testStorageProvider ) ;
63+ const client = BacktraceTestClient . buildFakeClient ( undefined , undefined , undefined , undefined , [ database ] ) ;
64+ try {
65+ const databaseStartResult = database . initialize ( ) ;
66+
67+ expect ( databaseStartResult ) . toBeFalsy ( ) ;
68+ expect ( database . enabled ) . toBeFalsy ( ) ;
69+ } finally {
70+ client . dispose ( ) ;
71+ }
9072 } ) ;
9173
9274 it ( 'Should not enable the database if the storage is not prepared' , ( ) => {
@@ -97,12 +79,6 @@ describe('Database setup tests', () => {
9779 path : '/path/to/fake/dir' ,
9880 } ,
9981 testStorageProvider ,
100- new RequestBacktraceReportSubmission (
101- {
102- url : TEST_SUBMISSION_URL ,
103- } ,
104- testHttpClient ,
105- ) ,
10682 ) ;
10783 jest . spyOn ( testStorageProvider , 'start' ) . mockReturnValue ( false ) ;
10884
@@ -114,35 +90,22 @@ describe('Database setup tests', () => {
11490
11591 it ( 'Should be disabled after disposing database' , ( ) => {
11692 const testStorageProvider = getTestStorageProvider ( ) ;
117- const database = new BacktraceDatabase (
118- undefined ,
119- testStorageProvider ,
120- new RequestBacktraceReportSubmission (
121- {
122- url : TEST_SUBMISSION_URL ,
123- } ,
124- testHttpClient ,
125- ) ,
126- ) ;
93+ const database = new BacktraceDatabase ( undefined , testStorageProvider ) ;
94+ const client = BacktraceTestClient . buildFakeClient ( undefined , undefined , undefined , undefined , [ database ] ) ;
12795
128- database . initialize ( ) ;
129- database . dispose ( ) ;
96+ try {
97+ database . initialize ( ) ;
98+ database . dispose ( ) ;
13099
131- expect ( database . enabled ) . toBeFalsy ( ) ;
100+ expect ( database . enabled ) . toBeFalsy ( ) ;
101+ } finally {
102+ client . dispose ( ) ;
103+ }
132104 } ) ;
133105
134106 it ( 'Should not add a record to disabled database' , ( ) => {
135107 const testStorageProvider = getTestStorageProvider ( ) ;
136- const database = new BacktraceDatabase (
137- undefined ,
138- testStorageProvider ,
139- new RequestBacktraceReportSubmission (
140- {
141- url : TEST_SUBMISSION_URL ,
142- } ,
143- testHttpClient ,
144- ) ,
145- ) ;
108+ const database = new BacktraceDatabase ( undefined , testStorageProvider ) ;
146109
147110 const result = database . add ( { } as BacktraceData , [ ] ) ;
148111 expect ( result ) . toBeFalsy ( ) ;
@@ -160,23 +123,22 @@ describe('Database setup tests', () => {
160123 autoSend : false ,
161124 } ,
162125 testStorageProvider ,
163- new RequestBacktraceReportSubmission (
164- {
165- url : TEST_SUBMISSION_URL ,
166- } ,
167- testHttpClient ,
168- ) ,
169126 ) ;
127+ const client = BacktraceTestClient . buildFakeClient ( undefined , undefined , undefined , undefined , [ database ] ) ;
170128
171- const databaseStartResult = database . initialize ( ) ;
129+ try {
130+ const databaseStartResult = database . initialize ( ) ;
172131
173- const nextTickAsync = promisify ( nextTick ) ;
174- await nextTickAsync ( ) ;
132+ const nextTickAsync = promisify ( nextTick ) ;
133+ await nextTickAsync ( ) ;
175134
176- expect ( databaseStartResult ) . toBeTruthy ( ) ;
177- expect ( database . enabled ) . toBeTruthy ( ) ;
135+ expect ( databaseStartResult ) . toBeTruthy ( ) ;
136+ expect ( database . enabled ) . toBeTruthy ( ) ;
178137
179- expect ( contextLoad ) . toHaveBeenCalledWith ( reports ) ;
138+ expect ( contextLoad ) . toHaveBeenCalledWith ( reports ) ;
139+ } finally {
140+ client . dispose ( ) ;
141+ }
180142 } ) ;
181143
182144 it ( 'should add no more than maximumNumberOfRecords reports from storage on initialize' , async ( ) => {
@@ -193,23 +155,22 @@ describe('Database setup tests', () => {
193155 maximumNumberOfRecords : 2 ,
194156 } ,
195157 testStorageProvider ,
196- new RequestBacktraceReportSubmission (
197- {
198- url : TEST_SUBMISSION_URL ,
199- } ,
200- testHttpClient ,
201- ) ,
202158 ) ;
159+ const client = BacktraceTestClient . buildFakeClient ( undefined , undefined , undefined , undefined , [ database ] ) ;
203160
204- const databaseStartResult = database . initialize ( ) ;
161+ try {
162+ const databaseStartResult = database . initialize ( ) ;
205163
206- const nextTickAsync = promisify ( nextTick ) ;
207- await nextTickAsync ( ) ;
164+ const nextTickAsync = promisify ( nextTick ) ;
165+ await nextTickAsync ( ) ;
208166
209- expect ( databaseStartResult ) . toBeTruthy ( ) ;
210- expect ( database . enabled ) . toBeTruthy ( ) ;
167+ expect ( databaseStartResult ) . toBeTruthy ( ) ;
168+ expect ( database . enabled ) . toBeTruthy ( ) ;
211169
212- expect ( contextLoad ) . toHaveBeenCalledWith ( expected ) ;
170+ expect ( contextLoad ) . toHaveBeenCalledWith ( expected ) ;
171+ } finally {
172+ client . dispose ( ) ;
173+ }
213174 } ) ;
214175
215176 it ( 'should limit report records by maximumNumberOfRecords and attachment records by maximumNumberOfAttachmentRecords' , async ( ) => {
@@ -241,22 +202,21 @@ describe('Database setup tests', () => {
241202 maximumNumberOfAttachmentRecords : 3 ,
242203 } ,
243204 testStorageProvider ,
244- new RequestBacktraceReportSubmission (
245- {
246- url : TEST_SUBMISSION_URL ,
247- } ,
248- testHttpClient ,
249- ) ,
250205 ) ;
206+ const client = BacktraceTestClient . buildFakeClient ( undefined , undefined , undefined , undefined , [ database ] ) ;
251207
252- const databaseStartResult = database . initialize ( ) ;
208+ try {
209+ const databaseStartResult = database . initialize ( ) ;
253210
254- const nextTickAsync = promisify ( nextTick ) ;
255- await nextTickAsync ( ) ;
211+ const nextTickAsync = promisify ( nextTick ) ;
212+ await nextTickAsync ( ) ;
256213
257- expect ( databaseStartResult ) . toBeTruthy ( ) ;
258- expect ( database . enabled ) . toBeTruthy ( ) ;
214+ expect ( databaseStartResult ) . toBeTruthy ( ) ;
215+ expect ( database . enabled ) . toBeTruthy ( ) ;
259216
260- expect ( contextLoad ) . toHaveBeenCalledWith ( expected ) ;
217+ expect ( contextLoad ) . toHaveBeenCalledWith ( expected ) ;
218+ } finally {
219+ client . dispose ( ) ;
220+ }
261221 } ) ;
262222} ) ;
0 commit comments