brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #480: Config self reference fix
Date Fri, 17 Feb 2017 23:40:58 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/480#discussion_r101855532
  
    --- Diff: core/src/main/java/org/apache/brooklyn/util/core/task/InterruptingImmediateSupplier.java
---
    @@ -0,0 +1,103 @@
    +/*
    + * 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.brooklyn.util.core.task;
    +
    +import java.util.concurrent.Callable;
    +import java.util.concurrent.Semaphore;
    +
    +import org.apache.brooklyn.util.exceptions.Exceptions;
    +import org.apache.brooklyn.util.exceptions.RuntimeInterruptedException;
    +import org.apache.brooklyn.util.guava.Maybe;
    +
    +import com.google.common.annotations.Beta;
    +import com.google.common.base.Supplier;
    +
    +/**
    + * Wraps a {@link Supplier} as an {@link ImmediateSupplier} by interrupting the thread
before calling {@link Supplier#get()}.
    + * If the call succeeds, the result is returned.
    + * If the call throws any trace including an {@link InterruptedException} or {@link RuntimeInterruptedException}

    + * (ie the call failed due to the interruption, typically because it tried to wait) 
    + * then this class concludes that there is no value available immediately and returns
{@link Maybe#absent()}.
    + * If the call throws any other error, that is returned.
    + * The interruption is cleared afterwards (unless the thread was interrupted when the
method was entered).
    + * <p>
    + * Note that some "immediate" methods, such as {@link Semaphore#acquire()} when a semaphore
is available,
    + * will throw if the thread is interrupted.  Typically there are workarounds, for instance:
    + * <code>if (semaphore.tryAcquire()) semaphore.acquire();</code>. 
    + */
    +@Beta
    +public class InterruptingImmediateSupplier<T> implements ImmediateSupplier<T>,
DeferredSupplier<T> {
    --- End diff --
    
    I'd add a test specifically for `InterruptingImmediateSupplierTest` (rather than just
testing it indirectly in `EntityConfigTest`). For example:
    ```
    public class InterruptingImmediateSupplierTest {
    
        @Test(expectedExceptions=UnsupportedOperationException.class)
        public void testOfInvalidType() throws Exception {
            InterruptingImmediateSupplier.of("myval");
        }
        
        @Test
        public void testRunnable() throws Exception {
            assertImmediatelyPresent(Runnables.doNothing(), null);
            assertImmediatelyAbsent(new SleepingRunnable());
            assertImmediatelyFails(new FailingRunnable(), MarkerException.class);
        }
        
        @Test
        public void testCallable() throws Exception {
            assertImmediatelyPresent(Callables.returning("myval"), "myval");
            assertImmediatelyAbsent(new SleepingCallable());
            assertImmediatelyFails(new FailingCallable(), MarkerException.class);
        }
        
        @Test
        public void testSupplier() throws Exception {
            assertImmediatelyPresent(Suppliers.ofInstance("myval"), "myval");
            assertImmediatelyAbsent(new SleepingSupplier());
            assertImmediatelyFails(new FailingSupplier(), MarkerException.class);
        }
        
        private void assertImmediatelyPresent(Object orig, Object expected) {
            Maybe<Object> result = getImmediately(orig);
            assertEquals(result.get(), expected);
            assertFalse(Thread.currentThread().isInterrupted());
        }
        
        private void assertImmediatelyAbsent(Object orig) {
            Maybe<Object> result = getImmediately(orig);
            assertTrue(result.isAbsent(), "result="+result);
            assertFalse(Thread.currentThread().isInterrupted());
        }
        
        private void assertImmediatelyFails(Object orig, Class<? extends Exception>
expected) {
            try {
                Maybe<Object> result = getImmediately(orig);
                Asserts.shouldHaveFailedPreviously("result="+result);
            } catch (Exception e) {
                Asserts.expectedFailureOfType(e, expected);
            }
            assertFalse(Thread.currentThread().isInterrupted());
        }
        
        private Maybe<Object> getImmediately(Object val) {
            InterruptingImmediateSupplier<Object> supplier = InterruptingImmediateSupplier.of(val);
            return supplier.getImmediately();
        }
        
        public static class SleepingRunnable implements Runnable {
            @Override public void run() {
                Time.sleep(Duration.ONE_MINUTE);
            }
        }
        
        public static class SleepingCallable implements Callable<Void> {
            @Override public Void call() {
                Time.sleep(Duration.ONE_MINUTE);
                return null;
            }
        }
        
        public static class SleepingSupplier implements Supplier<Void> {
            @Override public Void get() {
                Time.sleep(Duration.ONE_MINUTE);
                return null;
            }
        }
        
        public static class FailingRunnable implements Runnable {
            @Override public void run() {
                throw new MarkerException();
            }
        }
        
        public static class FailingCallable implements Callable<Void> {
            @Override public Void call() {
                throw new MarkerException();
            }
        }
        
        public static class FailingSupplier implements Supplier<Void> {
            @Override public Void get() {
                throw new MarkerException();
            }
        }
        
        public static class MarkerException extends RuntimeException {
            private static final long serialVersionUID = -3395361406478634652L;
        }
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message