Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4f12dff71606c4e3e6df1e42b21f85dccb10795e (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
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2013, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Christian W. Damus (CEA LIST) - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.signal.RemoteException;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.collection.CloseableIterator;
import org.eclipse.net4j.util.concurrent.ExecutorServiceFactory;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Bug 421287: [Query] Failure to execute query results in hung async iterator
 *
 * @author Christian W. Damus (CEA LIST)
 */
public class Bugzilla_421287_Test extends AbstractCDOTest
{
  private static final ThreadLocal<Boolean> IS_RUNNING_IN_BACKGROUND = new ThreadLocal<Boolean>()
  {
    @Override
    protected Boolean initialValue()
    {
      return false;
    }
  };

  public void testSyncQueryForUnknownEClass() throws Exception
  {
    if (runningAsync())
    {
      CDOSession session = openSession();
      CDOView view = session.openView();

      try
      {
        List<?> results = view.queryInstances(createDynamicEClass());
        assertEquals(true, results.isEmpty());
      }
      catch (RemoteException ex)
      {
        // This is expected (though I would argue it should not be)
        assertEquals(true, StringUtil.safe(ex.getLocalizedMessage()).contains("Package not found"));
      }
    }
  }

  public void testAsyncQueryForUnknownEClass() throws Exception
  {
    if (runningAsync())
    {
      CDOSession session = openSession();
      CDOView view = session.openView();

      CloseableIterator<?> results = view.queryInstancesAsync(createDynamicEClass());

      try
      {
        assertEquals(false, results.hasNext());
      }
      finally
      {
        results.close();
      }
    }
  }

  private boolean runningAsync()
  {
    final boolean result = IS_RUNNING_IN_BACKGROUND.get();

    if (!result)
    {
      try
      {
        final Method method = getClass().getMethod(getName());
        executeAsync(new Callable<Void>()
        {
          public Void call() throws Exception
          {
            IS_RUNNING_IN_BACKGROUND.set(true);

            try
            {
              method.invoke(Bugzilla_421287_Test.this);
            }
            catch (InvocationTargetException ex)
            {
              if (ex.getTargetException() instanceof Exception)
              {
                throw (Exception)ex.getTargetException();
              }
              else if (ex.getTargetException() instanceof Error)
              {
                throw (Error)ex.getTargetException();
              }
              else
              {
                throw ex;
              }

            }
            return null;
          }
        });
      }
      catch (Exception ex)
      {
        fail("Test reflection failure: " + ex.getLocalizedMessage());
      }
    }

    return result;
  }

  private void executeAsync(Callable<?> assertions)
  {
    Future<?> future = ExecutorServiceFactory.get(getClientContainer()).submit(assertions);

    try
    {
      future.get(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    }
    catch (TimeoutException ex)
    {
      fail("Test timed out");
    }
    catch (InterruptedException ex)
    {
      fail("Test interrupted");
    }
    catch (ExecutionException ex)
    {
      if (ex.getCause() instanceof RuntimeException)
      {
        throw (RuntimeException)ex.getCause();
      }
      else if (ex.getCause() instanceof Error)
      {
        throw (Error)ex.getCause();
      }
      else
      {
        fail("Unexpected exception in test: " + ex.getCause());
      }
    }
  }

  private EClass createDynamicEClass()
  {
    EPackage ePackage = createUniquePackage();

    EClass result = EcoreFactory.eINSTANCE.createEClass();
    result.setName("Dynamic");
    ePackage.getEClassifiers().add(result);
    return result;
  }
}

Back to the top