/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2021, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.test.clustering.cluster.ejb.timer.beans;

import java.util.function.Function;

import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;

/**
 * @author Paul Ferraro
 */
public abstract class AbstractManualTimerBean extends AbstractTimerBean implements ManualTimerBean {

    private final Function<TimerService, Timer> factory;

    protected AbstractManualTimerBean(Function<TimerService, Timer> factory) {
        this.factory = factory;
    }

    @Override
    public void createTimer() {
        this.factory.apply(this.service);
        java.util.Collection<Timer> timers = this.service.getTimers();
        assert timers.size() == 1 : timers.toString();
    }

    @Override
    public void cancel() {
        for (Timer timer : this.service.getTimers()) {
            timer.cancel();
        }
    }

    @Timeout
    public void timeout(Timer timer) {
        this.record(timer);
    }
}

@@ -1117,16 +1117,61 @@
 ction;%0A%0A
+import javax.ejb.NoSuchObjectLocalException;%0A
 import j
@@ -1888,22 +1888,382 @@
    t
-imer.cancel();
+ry %7B%0A                timer.cancel();%0A            %7D catch (NoSuchObjectLocalException e) %7B%0A                // This can happen if timer expired concurrently%0A                // In this case, verify that a fresh call to getTimers() no longer returns it.%0A                if (this.service.getTimers().contains(timer)) %7B%0A                    throw e;%0A                %7D%0A            %7D
 %0A   

Handle race condition if timer expired since call to TimerService#getTimers()