使用多线程和实体管理器的Junit测试不起作用

时间:2014-04-17 13:02:21

标签: java multithreading unit-testing spring-mvc eclipselink

我的客户希望我为System的测试性能编写一个测试。但我有实体经理的问题。 我有一种商业方法:

@Transactional(isolation = Isolation.READ_COMMITTED)
public ProductSequence generate(String prefix) {
    if (StringUtils.isBlank(prefix)) {
        throw new IllegalArgumentException("prefix is blank");
    }
    ProductSequence productSequence = new ProductSequence();
    List<ProductSequence> lstProductSequences = getProductSequenceByPrefix(prefix);

    if (!lstProductSequences.isEmpty()) {
        productSequence = lstProductSequences.get(0);
        productSequence.setSequence(productSequence.getSequence() + 1);
        this.entityManager.merge(productSequence);

    } else {
        productSequence.setPrefix(prefix);
        productSequence.setSequence(1L);
        this.entityManager.persist(productSequence);
//want to check again if data was persisted or not 
        List<ProductSequence> lstProductSequences2 = getProductSequenceByPrefix(prefix);
        if (lstProductSequences2.isEmpty()) {
            System.out.println("cannt persist entity manager");
        }

    }
    return productSequence;

}

/**
 * get product list by prefix.
 * 
 * @param prefix
 *            <code>String</code>
 * @return List<ProductSequence>
 */
@Transactional
public List<ProductSequence> getProductSequenceByPrefix(String prefix) {
    TypedQuery<ProductSequence> typedQuery = entityManager.createQuery(
            "SELECT o FROM ProductSequence AS o WHERE o.prefix = :prefix", ProductSequence.class);

    typedQuery.setParameter("prefix", prefix);
    return typedQuery.getResultList();
}

以下是这些方法的测试方法

  final String prefix = "TEST-AUTO";
final List<String> tags = new LinkedList<String>();
@Test
public void testGenerateWithThread() {



    final int loops = 5;

    final int threadCount = 1;

    final CyclicBarrier barrier = new CyclicBarrier(threadCount + 1);

    final boolean[] error = new boolean[] { false };

    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threads.length; i++) {
        final int t = i;
        Runnable r = new Runnable() {
            @Override
            @Transactional
            public void run() {
                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    error[0] = true;
                } catch (BrokenBarrierException e) {
                    error[0] = true;
                }
                for (int j = 0; j < loops; j++) {
                    String tagGenerate = productSequence.generate(prefix);
                    tags.add(tagGenerate);
                    System.out.println("In thread " + t + ": " + tagGenerate);
                }
            }
        };
        threads[i] = new Thread(r);
        threads[i].start();
    }

    try {
        barrier.await();
    } catch (InterruptedException e1) {
        System.out.println("Interrupted whilst waiting for barrier");
    } catch (BrokenBarrierException e1) {
        System.out.println("Broken barrier");
    }

    for (int i = 0; i < threads.length; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            System.out.println("Interrupted whilst joining thread " + i);
        }
    }

    assertEquals(threadCount * loops, tags.size());
}

/**
 * test for generate function
 */
@Test
@Transactional
public void testGenerateWithoutThread() {
    for (int i = 1; i <= 5; i++) {
        String tagGenerate = productSequence.generate(prefix);
        System.out.println("tag gen " + tagGenerate);
        assertEquals(prefix + "-" + i, tagGenerate);
    }
}

问题是当测试运行时,testGenerateWithoutThread方法工作正常,但testGenerateWithThread无法将数据持久存储到内存数据库中。运行testGenerateWitThread为“TEST-AUTO-1”时,所有标签值列表,但在testGenerateWithoutThread中将为“TEST-AUTO-1”,“TEST-AUTO-2”,“TEST-AUTO-3”,“TEST-AUTO” -4“,”TEST-AUTO-5“。 我不知道为什么?当我运行多线程测试时,实体管理器无法将数据保存到内存数据库中,但正常的方法可以吗?

请帮帮我!非常感谢!

1 个答案:

答案 0 :(得分:0)

testGenerateWithThread()需要@Transactional标记,因为您在TestGenerateWithoutThread上有此标记。请尝试以下方法:

@Test @Transactional public void TestGenerateWithThread() {

    final int loops = 5;

    final int threadCount = 1;

    final CyclicBarrier barrier = new CyclicBarrier(threadCount + 1);

    final boolean[] error = new boolean[] { false };

    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threads.length; i++) {
        final int t = i;
        Runnable r = new Runnable() {
            @Override
            @Transactional
            public void run() {
                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    error[0] = true;
                } catch (BrokenBarrierException e) {
                    error[0] = true;
                }
                for (int j = 0; j < loops; j++) {
                    String tagGenerate = productSequence.generate(prefix);
                    tags.add(tagGenerate);
                    System.out.println("In thread " + t + ": " + tagGenerate);
                }
            }
        };
        threads[i] = new Thread(r);
        threads[i].start();
    }

    try {
        barrier.await();
    } catch (InterruptedException e1) {
        System.out.println("Interrupted whilst waiting for barrier");
    } catch (BrokenBarrierException e1) {
        System.out.println("Broken barrier");
    }

    for (int i = 0; i < threads.length; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            System.out.println("Interrupted whilst joining thread " + i);
        }
    }

    assertEquals(threadCount * loops, tags.size());
}