/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.processor.aggregator;

import org.apache.camel.AggregationStrategy;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.jupiter.api.Test;

/**
 * Based on user forum issue
 */
public class AggregateLostGroupIssueTest extends ContextTestSupport {

    private int messageIndex;

    @Test
    public void testAggregateLostGroupIssue() throws Exception {
        messageIndex = 0;

        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedMessageCount(2);
        mock.message(0).body().isEqualTo("0,1,2,3,4,5,6,7,8,9");
        mock.message(1).body().isEqualTo("10,11,12,13,14,15,16,17,18,19");

        assertMockEndpointsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("timer://foo?period=10&delay=0").startupOrder(2).process(new Processor() {
                    public void process(Exchange exchange) throws Exception {
                        exchange.getMessage().setBody(messageIndex++);
                        exchange.getMessage().setHeader("aggregateGroup", "group1");
                    }
                }).to("direct:aggregator");

                from("direct:aggregator").startupOrder(1).aggregate(header("aggregateGroup"), new AggregationStrategy() {
                    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                        if (oldExchange == null) {
                            return newExchange;
                        }

                        String oldBody = oldExchange.getIn().getBody(String.class);
                        String newBody = newExchange.getIn().getBody(String.class);

                        oldExchange.getIn().setBody(oldBody + "," + newBody);
                        return oldExchange;
                    }
                }).completionSize(10).completionTimeout(200).completionTimeoutCheckerInterval(10).to("log:aggregated").to("mock:result");
            }
        };
    }

}

@@ -1101,16 +1101,170 @@
 dpoint;%0A
+import org.apache.camel.processor.aggregate.MemoryAggregationRepository;%0Aimport org.junit.jupiter.api.AfterEach;%0Aimport org.junit.jupiter.api.BeforeEach;%0A
 import o
@@ -1428,16 +1428,486 @@
 geIndex;
+%0A    private MemoryAggregationRepository aggregationRepository;%0A%0A    @BeforeEach%0A    public void setUp() throws Exception %7B%0A        messageIndex = 0;%0A        super.setUp();%0A        getAggregationRepository().start();%0A        context.getRouteController().startRoute(%22foo%22);%0A    %7D%0A%0A    @AfterEach%0A    public void tearDown() throws Exception %7B%0A        context.getRouteController().stopRoute(%22foo%22);%0A        getAggregationRepository().stop();%0A        super.tearDown();%0A    %7D
 %0A%0A    @T
@@ -1979,35 +1979,8 @@
 n %7B%0A
-        messageIndex = 0;%0A%0A
     
@@ -2257,24 +2257,279 @@
 d();%0A    %7D%0A%0A
+    protected synchronized MemoryAggregationRepository getAggregationRepository() %7B%0A        if (aggregationRepository == null) %7B%0A            aggregationRepository = new MemoryAggregationRepository();%0A        %7D%0A        return aggregationRepository;%0A    %7D%0A%0A
     @Overrid
@@ -2764,16 +2764,26 @@
 lay=0%22).
+id(%22foo%22).
 startupO
@@ -3788,16 +3788,123 @@
       %7D)
+.aggregationRepository(AggregateLostGroupIssueTest.this::getAggregationRepository)%0A                        
 .complet

Improve stability of AggregateLostGroupIssueTest for rerun