aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/transaction/NestedTransactionTestCase.java
blob: 2bcadc5e536971bde82df377dcec1e139011e962 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.transaction;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bratseth
 */
public class NestedTransactionTestCase {

    @Test
    public void testNestedTransaction() {
        NestedTransaction t = new NestedTransaction();
        t.add(new TransactionTypeB("B1"), TransactionTypeC.class);
        t.add(new TransactionTypeC("C1"));
        t.add(new TransactionTypeA("A1"), TransactionTypeB.class);
        t.add(new TransactionTypeA("A2"));
        t.add(new TransactionTypeC("C2"));

        // Add two tasks to run after commit
        MutableInteger tasksRun = new MutableInteger();
        t.onCommitted(() -> { tasksRun.value++; });
        t.onCommitted(() -> { tasksRun.value++; });

        assertEquals(3, t.transactions().size());
        assertEquals(TransactionTypeA.class, t.transactions().get(0).getClass());
        assertEquals(TransactionTypeB.class, t.transactions().get(1).getClass());
        assertEquals(TransactionTypeC.class, t.transactions().get(2).getClass());

        assertEquals("A1", ((MockOperation)t.transactions().get(0).operations().get(0)).name);
        assertEquals("A2", ((MockOperation)t.transactions().get(0).operations().get(1)).name);
        assertEquals("B1", ((MockOperation)t.transactions().get(1).operations().get(0)).name);
        assertEquals("C1", ((MockOperation)t.transactions().get(2).operations().get(0)).name);
        assertEquals("C2", ((MockOperation)t.transactions().get(2).operations().get(1)).name);

        t.commit();
        assertTrue(((MockTransaction)t.transactions().get(0)).committed);
        assertTrue(((MockTransaction)t.transactions().get(1)).committed);
        assertTrue(((MockTransaction)t.transactions().get(2)).committed);
        assertEquals("After commit tasks are run", 2, tasksRun.value);
    }

    @Test
    public void testNestedTransactionFailingOnCommit() {
        NestedTransaction t = new NestedTransaction();
        t.add(new TransactionTypeC("C1"));
        t.add(new TransactionTypeA("A1"), TransactionTypeB.class, FailAtCommitTransactionType.class);
        t.add(new TransactionTypeA("A2"));
        t.add(new FailAtCommitTransactionType("Fail"), TransactionTypeC.class);
        t.add(new TransactionTypeC("C2"));
        t.add(new TransactionTypeB("B1"), TransactionTypeC.class, FailAtCommitTransactionType.class);

        // Add task to run after commit
        MutableInteger tasksRun = new MutableInteger();
        t.onCommitted(() -> {
            tasksRun.value++;
        });

        assertEquals(4, t.transactions().size());
        assertEquals(TransactionTypeA.class, t.transactions().get(0).getClass());
        assertEquals(TransactionTypeB.class, t.transactions().get(1).getClass());
        assertEquals(FailAtCommitTransactionType.class, t.transactions().get(2).getClass());
        assertEquals(TransactionTypeC.class, t.transactions().get(3).getClass());

        try { t.commit(); } catch (IllegalStateException expected) { }
        assertTrue(((MockTransaction)t.transactions().get(0)).rolledback);
        assertTrue(((MockTransaction)t.transactions().get(1)).rolledback);
        assertFalse(((MockTransaction) t.transactions().get(2)).committed);
        assertEquals("After commit tasks are not run", 0, tasksRun.value);
    }

    @Test
    public void testConflictingOrdering() {
        NestedTransaction t = new NestedTransaction();
        t.add(new TransactionTypeA("A1"), TransactionTypeB.class);
        t.add(new TransactionTypeB("B1"), TransactionTypeC.class);
        t.add(new TransactionTypeC("C1"), TransactionTypeA.class);
        try {
            t.commit();
            fail("Expected exception");
        }
        catch (IllegalStateException expected) {
        }
    }

    @Test
    public void testMoreThanOneCommitThrows() {
        NestedTransaction t = new NestedTransaction();
        t.add(new TransactionTypeA("A1"), TransactionTypeB.class);
        t.commit();
        try {
            t.commit();
            fail("Expected exception");
        }
        catch (IllegalStateException expected) {
        }
    }

    private static class TransactionTypeA extends MockTransaction {
        public TransactionTypeA(String name) { super(name); }
    }

    private static class TransactionTypeB extends MockTransaction {
        public TransactionTypeB(String name) { super(name); }
    }

    private static class TransactionTypeC extends MockTransaction {
        public TransactionTypeC(String name) { super(name); }
    }

    private static class FailAtCommitTransactionType extends MockTransaction {
        public FailAtCommitTransactionType(String name) { super(name); }
        @Override
        public void commit() {
            throw new RuntimeException();
        }
    }

    private static class MockTransaction extends AbstractTransaction {

        public boolean prepared = false, committed = false, rolledback = false;

        public MockTransaction(String name) {
            add(new MockOperation(name));
        }

        @Override
        public void prepare() {
            prepared = true;
        }

        @Override
        public void commit() {
            if ( ! prepared)
                throw new IllegalStateException("Commit before prepare");
            committed = true;
        }

        @Override
        public void rollbackOrLog() {
            if ( ! committed)
                throw new IllegalStateException("Rollback before commit");
            rolledback = true;
        }

    }

    private static class MockOperation implements Transaction.Operation {

        public String name;

        public MockOperation(String name) {
            this.name = name;
        }

    }

    private static class MutableInteger {

        public int value = 0;

    }

}