@@ -1687,6 +1687,171 @@ describe('ErrsolePostgres', () => {
1687
1687
} ) ;
1688
1688
} ) ;
1689
1689
1690
+ describe ( '#flushLogs' , ( ) => {
1691
+ let fakeRelease ;
1692
+ let fakeClient ;
1693
+
1694
+ beforeEach ( ( ) => {
1695
+ // Set the logs table and clear pendingLogs and reset the connection flag.
1696
+ errsolePostgres . logsTable = 'errsole_logs_v3' ;
1697
+ errsolePostgres . pendingLogs = [ ] ;
1698
+ errsolePostgres . isConnectionInProgress = false ;
1699
+
1700
+ // Reset any previous mocks for pool.connect
1701
+ jest . spyOn ( errsolePostgres . pool , 'connect' ) . mockRestore ( ) ;
1702
+ } ) ;
1703
+
1704
+ it ( 'should return {} immediately if no logs to flush' , async ( ) => {
1705
+ errsolePostgres . pendingLogs = [ ] ; // Ensure no logs are pending
1706
+ const result = await errsolePostgres . flushLogs ( ) ;
1707
+ expect ( result ) . toEqual ( { } ) ;
1708
+ // Since there are no logs, pool.connect should not be called.
1709
+ expect ( errsolePostgres . pool . connect ) . not . toHaveBeenCalled ( ) ;
1710
+ } ) ;
1711
+
1712
+ it ( 'should flush logs and resolve {} when logs are present and query succeeds' , async ( ) => {
1713
+ const sampleLog = {
1714
+ timestamp : new Date ( '2023-01-01T00:00:00Z' ) ,
1715
+ hostname : 'localhost' ,
1716
+ pid : 1234 ,
1717
+ source : 'test' ,
1718
+ level : 'info' ,
1719
+ message : 'test message' ,
1720
+ meta : 'meta data' ,
1721
+ errsole_id : 'err1'
1722
+ } ;
1723
+ errsolePostgres . pendingLogs = [ sampleLog ] ;
1724
+
1725
+ fakeRelease = jest . fn ( ) ;
1726
+ fakeClient = {
1727
+ query : jest . fn ( ( query , queryParams , callback ) => {
1728
+ // Simulate a successful query execution
1729
+ callback ( null ) ;
1730
+ } )
1731
+ } ;
1732
+
1733
+ // Override pool.connect to simulate a callback-style connection.
1734
+ jest . spyOn ( errsolePostgres . pool , 'connect' ) . mockImplementation ( ( callback ) => {
1735
+ callback ( null , fakeClient , fakeRelease ) ;
1736
+ } ) ;
1737
+
1738
+ const result = await errsolePostgres . flushLogs ( ) ;
1739
+
1740
+ // Verify that pendingLogs is cleared after flush.
1741
+ expect ( errsolePostgres . pendingLogs ) . toEqual ( [ ] ) ;
1742
+ // Check that the client.query was called with a query that contains the INSERT statement.
1743
+ expect ( fakeClient . query ) . toHaveBeenCalledWith (
1744
+ expect . stringContaining ( 'INSERT INTO errsole_logs_v3' ) ,
1745
+ expect . any ( Array ) ,
1746
+ expect . any ( Function )
1747
+ ) ;
1748
+ // Verify that the client was released.
1749
+ expect ( fakeRelease ) . toHaveBeenCalled ( ) ;
1750
+ // Ensure the flushLogs method resolved with an empty object.
1751
+ expect ( result ) . toEqual ( { } ) ;
1752
+ } ) ;
1753
+
1754
+ it ( 'should wait if isConnectionInProgress is true and then flush logs' , async ( ) => {
1755
+ // Use fake timers to simulate waiting.
1756
+ jest . useFakeTimers ( ) ;
1757
+ errsolePostgres . isConnectionInProgress = true ;
1758
+
1759
+ const sampleLog = {
1760
+ timestamp : new Date ( '2023-01-01T00:00:00Z' ) ,
1761
+ hostname : 'localhost' ,
1762
+ pid : 1234 ,
1763
+ source : 'test' ,
1764
+ level : 'info' ,
1765
+ message : 'test message' ,
1766
+ meta : 'meta data' ,
1767
+ errsole_id : 'err1'
1768
+ } ;
1769
+ errsolePostgres . pendingLogs = [ sampleLog ] ;
1770
+
1771
+ fakeRelease = jest . fn ( ) ;
1772
+ fakeClient = {
1773
+ query : jest . fn ( ( query , queryParams , callback ) => {
1774
+ callback ( null ) ;
1775
+ } )
1776
+ } ;
1777
+
1778
+ jest . spyOn ( errsolePostgres . pool , 'connect' ) . mockImplementation ( ( callback ) => {
1779
+ callback ( null , fakeClient , fakeRelease ) ;
1780
+ } ) ;
1781
+
1782
+ // Start flushLogs. It should wait until isConnectionInProgress is false.
1783
+ const flushPromise = errsolePostgres . flushLogs ( ) ;
1784
+
1785
+ // After 150ms, mark connection as no longer in progress.
1786
+ setTimeout ( ( ) => {
1787
+ errsolePostgres . isConnectionInProgress = false ;
1788
+ } , 150 ) ;
1789
+
1790
+ // Fast-forward timers.
1791
+ jest . advanceTimersByTime ( 200 ) ;
1792
+ const result = await flushPromise ;
1793
+
1794
+ // Restore real timers.
1795
+ jest . useRealTimers ( ) ;
1796
+
1797
+ expect ( errsolePostgres . pendingLogs ) . toEqual ( [ ] ) ;
1798
+ expect ( fakeClient . query ) . toHaveBeenCalled ( ) ;
1799
+ expect ( fakeRelease ) . toHaveBeenCalled ( ) ;
1800
+ expect ( result ) . toEqual ( { } ) ;
1801
+ } ) ;
1802
+
1803
+ it ( 'should reject if pool.connect returns an error' , async ( ) => {
1804
+ const sampleLog = {
1805
+ timestamp : new Date ( '2023-01-01T00:00:00Z' ) ,
1806
+ hostname : 'localhost' ,
1807
+ pid : 1234 ,
1808
+ source : 'test' ,
1809
+ level : 'info' ,
1810
+ message : 'test message' ,
1811
+ meta : 'meta data' ,
1812
+ errsole_id : 'err1'
1813
+ } ;
1814
+ errsolePostgres . pendingLogs = [ sampleLog ] ;
1815
+
1816
+ const connectError = new Error ( 'Connection failed' ) ;
1817
+ jest . spyOn ( errsolePostgres . pool , 'connect' ) . mockImplementation ( ( callback ) => {
1818
+ callback ( connectError ) ;
1819
+ } ) ;
1820
+
1821
+ await expect ( errsolePostgres . flushLogs ( ) ) . rejects . toThrow ( 'Connection failed' ) ;
1822
+ } ) ;
1823
+
1824
+ it ( 'should reject if client.query returns an error' , async ( ) => {
1825
+ const sampleLog = {
1826
+ timestamp : new Date ( '2023-01-01T00:00:00Z' ) ,
1827
+ hostname : 'localhost' ,
1828
+ pid : 1234 ,
1829
+ source : 'test' ,
1830
+ level : 'info' ,
1831
+ message : 'test message' ,
1832
+ meta : 'meta data' ,
1833
+ errsole_id : 'err1'
1834
+ } ;
1835
+ errsolePostgres . pendingLogs = [ sampleLog ] ;
1836
+
1837
+ fakeRelease = jest . fn ( ) ;
1838
+ fakeClient = {
1839
+ query : jest . fn ( ( query , queryParams , callback ) => {
1840
+ // Simulate a query failure.
1841
+ callback ( new Error ( 'Query failed' ) ) ;
1842
+ } )
1843
+ } ;
1844
+
1845
+ jest . spyOn ( errsolePostgres . pool , 'connect' ) . mockImplementation ( ( callback ) => {
1846
+ callback ( null , fakeClient , fakeRelease ) ;
1847
+ } ) ;
1848
+
1849
+ await expect ( errsolePostgres . flushLogs ( ) ) . rejects . toThrow ( 'Query failed' ) ;
1850
+ // Even if the query fails, the client should be released.
1851
+ expect ( fakeRelease ) . toHaveBeenCalled ( ) ;
1852
+ } ) ;
1853
+ } ) ;
1854
+
1690
1855
afterAll ( ( ) => {
1691
1856
cronJob . stop ( ) ;
1692
1857
clearInterval ( errsolePostgres . flushIntervalId ) ;
0 commit comments