void testSessionEnd()

in plugin/tst/software/aws/toolkits/eclipse/amazonq/util/QInvocationSessionTest.java [187:258]


    void testSessionEnd() throws InterruptedException, ExecutionException {
        threadingUtilsMock = mockStatic(ThreadingUtils.class);
        threadingUtilsMock.when(() -> ThreadingUtils.executeAsyncTaskAndReturnFuture(any(Runnable.class)))
                .thenAnswer(new Answer<Future<?>>() {
                    @Override
                    public Future<?> answer(final InvocationOnMock invocation) throws Throwable {
                        Runnable runnable = invocation.getArgument(0);
                        Runnable wrapper = () -> {
                            mockLspProvider();
                            mockDisplayAsyncCall();
                            mockQEclipseEditorUtils();
                            runnable.run();
                        };
                        ExecutorService executor = Executors.newSingleThreadExecutor();
                        return executor.submit(wrapper);
                    }
                });

        QInvocationSession session = QInvocationSession.getInstance();
        session.start(MOCK_EDITOR);

        // We need to mock the Display here because the latter half of the update is
        // done on the UI thread
        inlineCompletionUtilsMock = mockStatic(InlineCompletionUtils.class);

        // Test case: when there are suggestions received
        inlineCompletionUtilsMock.when(() -> InlineCompletionUtils.cwParamsFromContext(any(ITextEditor.class),
                any(ITextViewer.class), any(Integer.class), any(InlineCompletionTriggerKind.class)))
                .thenReturn(POTENT_PARAM);
        session.invoke();
        session.awaitAllUnresolvedTasks();
        assertTrue(session.isActive());
        session.endImmediately();

        // Test case: when there are not suggestions received
        inlineCompletionUtilsMock.when(() -> InlineCompletionUtils.cwParamsFromContext(any(ITextEditor.class),
                any(ITextViewer.class), any(Integer.class), any(InlineCompletionTriggerKind.class)))
                .thenReturn(IMPOTENT_PARAM);
        session.start(MOCK_EDITOR);
        session.invoke();
        session.awaitAllUnresolvedTasks();
        assertTrue(!session.isActive());
        session.endImmediately();

        // Test case: calling end when there are still requests in flight
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1);
        threadingUtilsMock.when(() -> ThreadingUtils.executeAsyncTaskAndReturnFuture(any(Runnable.class)))
                .thenAnswer(new Answer<Future<?>>() {
                    @Override
                    public Future<?> answer(final InvocationOnMock invocation) throws Throwable {
                        Runnable runnable = () -> {
                            try {
                                queue.take();
                            } catch (InterruptedException e) {
                                // This will print stack traces from interrupted exception for when it gets terminated forcefully
                                // It does not mean the test has failed.
                                e.printStackTrace();
                            }
                        };
                        ExecutorService executor = Executors.newSingleThreadExecutor();
                        return executor.submit(runnable);
                    }
                });
        session.start(MOCK_EDITOR);
        session.invoke();
        session.end();
        assertTrue(session.isActive());

        // Test case: force end
        session.endImmediately();
        assertTrue(!session.isActive());
    }