samples/reentrant/test/org/apidesign/reentrant/CriticalSectionBase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 18:51:38 +0200
changeset 263 7e8e995065c5
parent 112 64308321f7bd
permissions -rw-r--r--
Tests of all modules are executed and can fail the build
jtulach@111
     1
package org.apidesign.reentrant;
jtulach@111
     2
jtulach@111
     3
import java.util.ArrayList;
jtulach@111
     4
import java.util.Iterator;
jtulach@111
     5
import java.util.List;
jtulach@111
     6
import org.junit.Test;
jtulach@111
     7
import static org.junit.Assert.*;
jtulach@111
     8
jtulach@111
     9
public abstract class CriticalSectionBase {
jtulach@111
    10
    protected abstract CriticalSection<Integer> create();
jtulach@112
    11
    protected boolean reentrantJustOnce() {
jtulach@112
    12
        return false;
jtulach@112
    13
    }
jtulach@111
    14
jtulach@112
    15
    // BEGIN: reentrant.ok.call
jtulach@111
    16
    @Test
jtulach@111
    17
    public void testCriticalSectionWith15() {
jtulach@111
    18
        final CriticalSection<Integer> cs = create();
jtulach@111
    19
        testFor15(cs);
jtulach@111
    20
    }
jtulach@112
    21
    // END: reentrant.ok.call
jtulach@111
    22
    
jtulach@111
    23
    final void testFor15(CriticalSection<Integer> cs) {
jtulach@111
    24
        cs.assignPilot(15);
jtulach@111
    25
jtulach@111
    26
        List<Integer> ints = new ArrayList<Integer>();
jtulach@111
    27
        ints.add(8);
jtulach@111
    28
        ints.add(11);
jtulach@111
    29
        ints.add(10);
jtulach@111
    30
        ints.add(5);
jtulach@111
    31
        ints.add(12);
jtulach@111
    32
        ints.add(18);
jtulach@111
    33
        ints.add(13);
jtulach@111
    34
        ints.add(7);
jtulach@111
    35
jtulach@111
    36
        int cnt = cs.sumBigger(ints);
jtulach@111
    37
jtulach@111
    38
        assertEquals("18 is bigger than 15", 1, cnt);
jtulach@111
    39
        
jtulach@111
    40
    }
jtulach@111
    41
    
jtulach@111
    42
    @Test 
jtulach@111
    43
    public void teaseCriticalSectionWithReentrantCalls() {
jtulach@263
    44
        if (Boolean.getBoolean("no.failures")) return;
jtulach@263
    45
        
jtulach@111
    46
        final CriticalSection<Integer> cs = create();
jtulach@111
    47
        
jtulach@111
    48
        cs.assignPilot(10);
jtulach@111
    49
        
jtulach@111
    50
        class ChangePilotTo15 implements Runnable {
jtulach@112
    51
            // BEGIN: reentrant.forbidden.call
jtulach@112
    52
            // if this runnable is called from inside the critical section,
jtulach@112
    53
            // and the locks are non-reentrant then it throws an exception
jtulach@111
    54
            public void run() {
jtulach@111
    55
                testFor15(cs);
jtulach@111
    56
            }
jtulach@112
    57
            // END: reentrant.forbidden.call
jtulach@111
    58
        }
jtulach@111
    59
        
jtulach@111
    60
        List<Integer> ints = new MyCollection(new ChangePilotTo15(), 3);
jtulach@111
    61
        ints.add(8);
jtulach@111
    62
        ints.add(11);
jtulach@111
    63
        ints.add(10);
jtulach@111
    64
        ints.add(5);
jtulach@111
    65
        ints.add(12);
jtulach@111
    66
        ints.add(18);
jtulach@111
    67
        ints.add(13);
jtulach@111
    68
        ints.add(7);
jtulach@111
    69
        
jtulach@111
    70
        int cnt = cs.sumBigger(ints);
jtulach@111
    71
        
jtulach@111
    72
        assertEquals("11, 12, 18, 13 are bigger than 10", 4, cnt);
jtulach@111
    73
        
jtulach@111
    74
        assertEquals("Global count is sum of ints(e.g. 4) plus result of testFor15(e.g. 1)", 5, cs.getCount());
jtulach@111
    75
    }
jtulach@111
    76
jtulach@111
    77
    class MyCollection extends ArrayList<Integer> {
jtulach@112
    78
        private Runnable callback;
jtulach@111
    79
        private final int callbackBeforeIndex;
jtulach@111
    80
jtulach@111
    81
        public MyCollection(Runnable callback, int callbackAtIndex) {
jtulach@111
    82
            this.callback = callback;
jtulach@111
    83
            this.callbackBeforeIndex = callbackAtIndex;
jtulach@111
    84
        }
jtulach@111
    85
jtulach@111
    86
        @Override
jtulach@111
    87
        public Iterator<Integer> iterator() {
jtulach@111
    88
            final Iterator<Integer> delegate = super.iterator();
jtulach@111
    89
            class It implements Iterator<Integer> {
jtulach@111
    90
                private int index;
jtulach@111
    91
                
jtulach@111
    92
                public boolean hasNext() {
jtulach@111
    93
                    return delegate.hasNext();
jtulach@111
    94
                }
jtulach@111
    95
jtulach@111
    96
                public Integer next() {
jtulach@111
    97
                    if (index++ == callbackBeforeIndex) {
jtulach@112
    98
                        if (callback != null) {
jtulach@112
    99
                            callback.run();
jtulach@112
   100
                        }
jtulach@112
   101
                        if (reentrantJustOnce()) {
jtulach@112
   102
                            callback = null;
jtulach@112
   103
                        }
jtulach@111
   104
                    }
jtulach@111
   105
                    
jtulach@111
   106
                    return delegate.next();
jtulach@111
   107
                }
jtulach@111
   108
jtulach@111
   109
                public void remove() {
jtulach@111
   110
                    delegate.remove();
jtulach@111
   111
                }
jtulach@111
   112
            } // end of It
jtulach@111
   113
            
jtulach@111
   114
            return new It();
jtulach@111
   115
        }
jtulach@111
   116
    }
jtulach@111
   117
}