|
78 | 78 | import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogEnabled; |
79 | 79 | import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogEnabledForStream; |
80 | 80 | import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogError; |
| 81 | +import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogErrorNoFlush; |
81 | 82 | import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogEvent; |
82 | 83 | import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogEventBridgeCorrelationId; |
83 | 84 | import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogEventEnvVar; |
@@ -108,6 +109,13 @@ void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTar |
108 | 109 | writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", null, true); |
109 | 110 | writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_EVENT", false, true); |
110 | 111 | writeStaticField(LoggingConstants.class, "POWERTOOLS_SAMPLING_RATE", null, true); |
| 112 | + |
| 113 | + // Reset buffer state for clean test isolation |
| 114 | + LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager(); |
| 115 | + if (loggingManager instanceof TestLoggingManager) { |
| 116 | + ((TestLoggingManager) loggingManager).resetBufferState(); |
| 117 | + } |
| 118 | + |
111 | 119 | try { |
112 | 120 | FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close(); |
113 | 121 | } catch (NoSuchFileException e) { |
@@ -717,6 +725,132 @@ void shouldLogCorrelationIdOnAppSyncEvent() throws IOException { |
717 | 725 | .containsEntry("correlation_id", eventId); |
718 | 726 | } |
719 | 727 |
|
| 728 | + @Test |
| 729 | + void shouldClearBufferAfterSuccessfulHandlerExecution() { |
| 730 | + // WHEN |
| 731 | + requestHandler.handleRequest(new Object(), context); |
| 732 | + |
| 733 | + // THEN |
| 734 | + LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager(); |
| 735 | + if (loggingManager instanceof TestLoggingManager) { |
| 736 | + assertThat(((TestLoggingManager) loggingManager).isBufferCleared()).isTrue(); |
| 737 | + } |
| 738 | + } |
| 739 | + |
| 740 | + @Test |
| 741 | + void shouldClearBufferAfterSuccessfulStreamHandlerExecution() throws IOException { |
| 742 | + // WHEN |
| 743 | + requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[] {}), new ByteArrayOutputStream(), |
| 744 | + context); |
| 745 | + |
| 746 | + // THEN |
| 747 | + LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager(); |
| 748 | + if (loggingManager instanceof TestLoggingManager) { |
| 749 | + assertThat(((TestLoggingManager) loggingManager).isBufferCleared()).isTrue(); |
| 750 | + } |
| 751 | + } |
| 752 | + |
| 753 | + @Test |
| 754 | + void shouldClearBufferAfterHandlerExceptionWithLogError() { |
| 755 | + // GIVEN |
| 756 | + requestHandler = new PowertoolsLogError(); |
| 757 | + |
| 758 | + // WHEN |
| 759 | + try { |
| 760 | + requestHandler.handleRequest("input", context); |
| 761 | + } catch (Exception e) { |
| 762 | + // ignore |
| 763 | + } |
| 764 | + |
| 765 | + // THEN |
| 766 | + LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager(); |
| 767 | + if (loggingManager instanceof TestLoggingManager) { |
| 768 | + assertThat(((TestLoggingManager) loggingManager).isBufferCleared()).isTrue(); |
| 769 | + } |
| 770 | + } |
| 771 | + |
| 772 | + @Test |
| 773 | + void shouldClearBufferAfterHandlerExceptionWithEnvVarLogError() { |
| 774 | + try { |
| 775 | + // GIVEN |
| 776 | + LoggingConstants.POWERTOOLS_LOG_ERROR = true; |
| 777 | + requestHandler = new PowertoolsLogEnabled(true); |
| 778 | + |
| 779 | + // WHEN |
| 780 | + try { |
| 781 | + requestHandler.handleRequest("input", context); |
| 782 | + } catch (Exception e) { |
| 783 | + // ignore |
| 784 | + } |
| 785 | + |
| 786 | + // THEN |
| 787 | + LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager(); |
| 788 | + if (loggingManager instanceof TestLoggingManager) { |
| 789 | + assertThat(((TestLoggingManager) loggingManager).isBufferCleared()).isTrue(); |
| 790 | + } |
| 791 | + } finally { |
| 792 | + LoggingConstants.POWERTOOLS_LOG_ERROR = false; |
| 793 | + } |
| 794 | + } |
| 795 | + |
| 796 | + @Test |
| 797 | + void shouldFlushBufferOnUncaughtErrorWhenEnabled() { |
| 798 | + // GIVEN |
| 799 | + requestHandler = new PowertoolsLogError(); |
| 800 | + |
| 801 | + // WHEN |
| 802 | + try { |
| 803 | + requestHandler.handleRequest("input", context); |
| 804 | + } catch (Exception e) { |
| 805 | + // ignore |
| 806 | + } |
| 807 | + |
| 808 | + // THEN |
| 809 | + LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager(); |
| 810 | + if (loggingManager instanceof TestLoggingManager) { |
| 811 | + assertThat(((TestLoggingManager) loggingManager).isBufferFlushed()).isTrue(); |
| 812 | + } |
| 813 | + } |
| 814 | + |
| 815 | + @Test |
| 816 | + void shouldNotFlushBufferOnUncaughtErrorWhenDisabled() { |
| 817 | + // GIVEN |
| 818 | + PowertoolsLogErrorNoFlush handler = new PowertoolsLogErrorNoFlush(); |
| 819 | + |
| 820 | + // WHEN |
| 821 | + try { |
| 822 | + handler.handleRequest("input", context); |
| 823 | + } catch (Exception e) { |
| 824 | + // ignore |
| 825 | + } |
| 826 | + |
| 827 | + // THEN |
| 828 | + LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager(); |
| 829 | + if (loggingManager instanceof TestLoggingManager) { |
| 830 | + assertThat(((TestLoggingManager) loggingManager).isBufferFlushed()).isFalse(); |
| 831 | + } |
| 832 | + } |
| 833 | + |
| 834 | + @Test |
| 835 | + void shouldClearBufferBeforeErrorLoggingWhenFlushBufferOnUncaughtErrorDisabled() { |
| 836 | + // GIVEN |
| 837 | + PowertoolsLogErrorNoFlush handler = new PowertoolsLogErrorNoFlush(); |
| 838 | + |
| 839 | + // WHEN |
| 840 | + try { |
| 841 | + handler.handleRequest("input", context); |
| 842 | + } catch (Exception e) { |
| 843 | + // ignore |
| 844 | + } |
| 845 | + |
| 846 | + // THEN - Buffer should be cleared and not flushed |
| 847 | + LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager(); |
| 848 | + if (loggingManager instanceof TestLoggingManager) { |
| 849 | + assertThat(((TestLoggingManager) loggingManager).isBufferCleared()).isTrue(); |
| 850 | + assertThat(((TestLoggingManager) loggingManager).isBufferFlushed()).isFalse(); |
| 851 | + } |
| 852 | + } |
| 853 | + |
720 | 854 | private void resetLogLevel(Level level) |
721 | 855 | throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
722 | 856 | Method setLogLevels = LambdaLoggingAspect.class.getDeclaredMethod("setLogLevels", Level.class); |
|
0 commit comments