|
1 | 1 | import { describe, it, expect, vi, afterEach } from "vitest"; |
2 | | -import { |
3 | | - EventProcessor, |
4 | | - TxOBEvent, |
5 | | - ErrorUnprocessableEventHandler, |
6 | | - defaultBackoff, |
7 | | -} from "./processor.js"; |
| 2 | +import { EventProcessor, TxOBEvent, defaultBackoff } from "./processor.js"; |
| 3 | +import { TxobError, ErrorUnprocessableEventHandler } from "./error.js"; |
8 | 4 | import { sleep } from "./sleep.js"; |
9 | 5 |
|
10 | 6 | const mockTxClient = { |
@@ -763,6 +759,206 @@ describe("EventProcessor - processEvents", () => { |
763 | 759 | "error processing event", |
764 | 760 | ); |
765 | 761 | }); |
| 762 | + |
| 763 | + it("should use the latest backoff when multiple TxobErrors have different backoffUntil dates", async () => { |
| 764 | + const opts = { |
| 765 | + maxErrors: 5, |
| 766 | + backoff: vi.fn(() => new Date(now.getTime() + 5000)), // Default backoff: 5 seconds |
| 767 | + pollingIntervalMs: 10, |
| 768 | + }; |
| 769 | + |
| 770 | + // Create different backoff times |
| 771 | + const backoff1 = new Date(now.getTime() + 10000); // 10 seconds |
| 772 | + const backoff2 = new Date(now.getTime() + 20000); // 20 seconds (latest) |
| 773 | + const backoff3 = new Date(now.getTime() + 15000); // 15 seconds |
| 774 | + |
| 775 | + const error1 = new TxobError("error 1", { backoffUntil: backoff1 }); |
| 776 | + const error2 = new TxobError("error 2", { backoffUntil: backoff2 }); |
| 777 | + const error3 = new TxobError("error 3", { backoffUntil: backoff3 }); |
| 778 | + |
| 779 | + const handlerMap = { |
| 780 | + evtType1: { |
| 781 | + handler1: vi.fn(() => Promise.reject(error1)), |
| 782 | + handler2: vi.fn(() => Promise.reject(error2)), |
| 783 | + handler3: vi.fn(() => Promise.reject(error3)), |
| 784 | + }, |
| 785 | + }; |
| 786 | + |
| 787 | + const evt1: TxOBEvent<keyof typeof handlerMap> = { |
| 788 | + type: "evtType1", |
| 789 | + id: "1", |
| 790 | + timestamp: now, |
| 791 | + data: {}, |
| 792 | + correlation_id: "abc123", |
| 793 | + handler_results: {}, |
| 794 | + errors: 0, |
| 795 | + }; |
| 796 | + |
| 797 | + const events = [evt1]; |
| 798 | + let callCount = 0; |
| 799 | + mockClient.getEventsToProcess.mockImplementation(() => { |
| 800 | + callCount++; |
| 801 | + return Promise.resolve(callCount === 1 ? events : []); |
| 802 | + }); |
| 803 | + mockTxClient.getEventByIdForUpdateSkipLocked.mockImplementation((id) => { |
| 804 | + return Promise.resolve(events.find((e) => e.id === id) ?? null); |
| 805 | + }); |
| 806 | + mockTxClient.updateEvent.mockImplementation(() => { |
| 807 | + return Promise.resolve(); |
| 808 | + }); |
| 809 | + |
| 810 | + const processor = new EventProcessor({ |
| 811 | + client: mockClient, |
| 812 | + handlerMap, |
| 813 | + ...opts, |
| 814 | + }); |
| 815 | + processor.start(); |
| 816 | + await sleep(50); // Wait for processing |
| 817 | + await processor.stop(); |
| 818 | + |
| 819 | + expect(mockClient.getEventsToProcess).toHaveBeenCalled(); |
| 820 | + expect(mockClient.transaction).toHaveBeenCalledTimes(1); |
| 821 | + |
| 822 | + // All handlers should have been called |
| 823 | + expect(handlerMap.evtType1.handler1).toHaveBeenCalledOnce(); |
| 824 | + expect(handlerMap.evtType1.handler2).toHaveBeenCalledOnce(); |
| 825 | + expect(handlerMap.evtType1.handler3).toHaveBeenCalledOnce(); |
| 826 | + |
| 827 | + // Default backoff should also be called |
| 828 | + expect(opts.backoff).toHaveBeenCalledWith(1); |
| 829 | + |
| 830 | + // The latest backoff (backoff2 = 20 seconds) should be used |
| 831 | + expect(mockTxClient.updateEvent).toHaveBeenCalledTimes(1); |
| 832 | + const updateCall = mockTxClient.updateEvent.mock.calls[0][0]; |
| 833 | + expect(updateCall.backoff_until).toEqual(backoff2); |
| 834 | + expect(updateCall.errors).toBe(1); |
| 835 | + }); |
| 836 | + |
| 837 | + it("should use the latest backoff when TxobError backoff is later than default backoff", async () => { |
| 838 | + const laterBackoff = new Date(now.getTime() + 30000); // 30 seconds |
| 839 | + const defaultBackoffTime = new Date(now.getTime() + 5000); // 5 seconds |
| 840 | + |
| 841 | + const opts = { |
| 842 | + maxErrors: 5, |
| 843 | + backoff: vi.fn(() => defaultBackoffTime), |
| 844 | + pollingIntervalMs: 10, |
| 845 | + }; |
| 846 | + |
| 847 | + const error = new TxobError("error with backoff", { |
| 848 | + backoffUntil: laterBackoff, |
| 849 | + }); |
| 850 | + |
| 851 | + const handlerMap = { |
| 852 | + evtType1: { |
| 853 | + handler1: vi.fn(() => Promise.reject(error)), |
| 854 | + }, |
| 855 | + }; |
| 856 | + |
| 857 | + const evt1: TxOBEvent<keyof typeof handlerMap> = { |
| 858 | + type: "evtType1", |
| 859 | + id: "1", |
| 860 | + timestamp: now, |
| 861 | + data: {}, |
| 862 | + correlation_id: "abc123", |
| 863 | + handler_results: {}, |
| 864 | + errors: 0, |
| 865 | + }; |
| 866 | + |
| 867 | + const events = [evt1]; |
| 868 | + let callCount = 0; |
| 869 | + mockClient.getEventsToProcess.mockImplementation(() => { |
| 870 | + callCount++; |
| 871 | + return Promise.resolve(callCount === 1 ? events : []); |
| 872 | + }); |
| 873 | + mockTxClient.getEventByIdForUpdateSkipLocked.mockImplementation((id) => { |
| 874 | + return Promise.resolve(events.find((e) => e.id === id) ?? null); |
| 875 | + }); |
| 876 | + mockTxClient.updateEvent.mockImplementation(() => { |
| 877 | + return Promise.resolve(); |
| 878 | + }); |
| 879 | + |
| 880 | + const processor = new EventProcessor({ |
| 881 | + client: mockClient, |
| 882 | + handlerMap, |
| 883 | + ...opts, |
| 884 | + }); |
| 885 | + processor.start(); |
| 886 | + await sleep(50); // Wait for processing |
| 887 | + await processor.stop(); |
| 888 | + |
| 889 | + expect(mockClient.transaction).toHaveBeenCalledTimes(1); |
| 890 | + expect(opts.backoff).toHaveBeenCalledWith(1); |
| 891 | + |
| 892 | + // The latest backoff (laterBackoff = 30 seconds) should be used, not the default (5 seconds) |
| 893 | + const updateCall = mockTxClient.updateEvent.mock.calls[0][0]; |
| 894 | + expect(updateCall.backoff_until).toEqual(laterBackoff); |
| 895 | + expect(updateCall.backoff_until?.getTime()).toBeGreaterThan( |
| 896 | + defaultBackoffTime.getTime(), |
| 897 | + ); |
| 898 | + }); |
| 899 | + |
| 900 | + it("should use default backoff when TxobError backoff is earlier than default backoff", async () => { |
| 901 | + const earlierBackoff = new Date(now.getTime() + 2000); // 2 seconds |
| 902 | + const defaultBackoffTime = new Date(now.getTime() + 5000); // 5 seconds |
| 903 | + |
| 904 | + const opts = { |
| 905 | + maxErrors: 5, |
| 906 | + backoff: vi.fn(() => defaultBackoffTime), |
| 907 | + pollingIntervalMs: 10, |
| 908 | + }; |
| 909 | + |
| 910 | + const error = new TxobError("error with backoff", { |
| 911 | + backoffUntil: earlierBackoff, |
| 912 | + }); |
| 913 | + |
| 914 | + const handlerMap = { |
| 915 | + evtType1: { |
| 916 | + handler1: vi.fn(() => Promise.reject(error)), |
| 917 | + }, |
| 918 | + }; |
| 919 | + |
| 920 | + const evt1: TxOBEvent<keyof typeof handlerMap> = { |
| 921 | + type: "evtType1", |
| 922 | + id: "1", |
| 923 | + timestamp: now, |
| 924 | + data: {}, |
| 925 | + correlation_id: "abc123", |
| 926 | + handler_results: {}, |
| 927 | + errors: 0, |
| 928 | + }; |
| 929 | + |
| 930 | + const events = [evt1]; |
| 931 | + let callCount = 0; |
| 932 | + mockClient.getEventsToProcess.mockImplementation(() => { |
| 933 | + callCount++; |
| 934 | + return Promise.resolve(callCount === 1 ? events : []); |
| 935 | + }); |
| 936 | + mockTxClient.getEventByIdForUpdateSkipLocked.mockImplementation((id) => { |
| 937 | + return Promise.resolve(events.find((e) => e.id === id) ?? null); |
| 938 | + }); |
| 939 | + mockTxClient.updateEvent.mockImplementation(() => { |
| 940 | + return Promise.resolve(); |
| 941 | + }); |
| 942 | + |
| 943 | + const processor = new EventProcessor({ |
| 944 | + client: mockClient, |
| 945 | + handlerMap, |
| 946 | + ...opts, |
| 947 | + }); |
| 948 | + processor.start(); |
| 949 | + await sleep(50); // Wait for processing |
| 950 | + await processor.stop(); |
| 951 | + |
| 952 | + expect(mockClient.transaction).toHaveBeenCalledTimes(1); |
| 953 | + expect(opts.backoff).toHaveBeenCalledWith(1); |
| 954 | + |
| 955 | + // The latest backoff (defaultBackoffTime = 5 seconds) should be used, not the earlier one (2 seconds) |
| 956 | + const updateCall = mockTxClient.updateEvent.mock.calls[0][0]; |
| 957 | + expect(updateCall.backoff_until).toEqual(defaultBackoffTime); |
| 958 | + expect(updateCall.backoff_until?.getTime()).toBeGreaterThan( |
| 959 | + earlierBackoff.getTime(), |
| 960 | + ); |
| 961 | + }); |
766 | 962 | }); |
767 | 963 |
|
768 | 964 | describe("defaultBackoff", () => { |
|
0 commit comments