2016-08-09 5 views
3

この3つのメソッドをそれぞれ10個の同時スレッドとして実行するJUnitベースのコードは何でしょうか。複数の同時スレッドでテスト・メソッドのセットを実行

@RunWith(SpringJUnit4ClassRunner.class 
@SpringBootTest 
public class TestClass { 


    @Test 
    public void readFromDBOneRecord(){ 
     try { 
     dbService.findOneByID("1"); 
     } catch (Exception error) { 
      Assert.fail("Unexpected error occured ."); 
     } 
    } 

    @Test 
    public void writeToDBOneRecord(){ 
     try { 
      dbService.save(entity.builder() 
          .setID("1").setName("John").build()) 
     } catch (Exception error) { 
      Assert.fail("Unexpected error occured ."); 
     } 
    } 

    @Test 
    public void deleteDbRecord(){ 
     try { 
      dbService.delete("1"); 
     } catch (Exception error) { 
      Assert.fail("Unexpected error occured ."); 
     } 
    } 
} 

場合によっては、一部のメソッドで例外がスローされることがあります。 deletewriteToDBOneRecordより前に実行されている場合と同様です。 だから配列は、メソッドごとにのみ3スレッドについて言うことになる例えば:

OperationNr|| OperationName || [ThreadNr/total threads per method]OperationType 

    1.  write  [2/3]w 
    2.  read   [1/3]r 
    3.  read   [3/3]r 
    4.  delete  [2/3]d 
    5.  read   [2/3]r 
    6.  delete  [3/3]d ->exception no record 
    7.  write  [1/3]w 
    8.  write  [3/3]w ->exception record already present 
    9.  delete  [1/3]d 

何だろう10件の同時スレッド(合計30)にそれぞれこの3試験方法を実行するためのコード?

+0

すべてを並行して実行したいですか? –

+0

すべてのスレッドはここで1と同じIDを使用しますか? –

+0

定義済みのスレッド数で各テストメソッドを実行したい。だから私は3つの同時ライタースレッドと同時に実行される5つの同時読み取りスレッドを持っていたい。したがって、2つのテストメソッドは並行して8つのスレッドによって同時に実行されます。 – mCs

答えて

2

あなたが並列にすべてをやりたいように、私はすべてをミックスし、次のようにスレッドを同期するCountDownLatchのインスタンスに依存しています:

@Test 
public void testMultiThreading() throws Exception { 
    // Total of reader threads 
    int reader = 5; 
    // Total of writer threads 
    int writer = 3; 
    // Total of remover threads 
    int remover = 1; 
    // CountDownLatch used to release all the threads at the same time 
    final CountDownLatch startSignal = new CountDownLatch(1); 
    // CountDownLatch used to be notified when all threads did their task 
    final CountDownLatch doneSignal = new CountDownLatch(reader + writer + remover); 
    // List in which we collect all the errors 
    final List<Exception> errors = Collections.synchronizedList(new ArrayList<>()); 
    // Create all the reader threads and start them 
    for (int i = 0; i < reader; i++) { 
     Thread thread = new Thread() { 
      public void run() { 
       try { 
        startSignal.await(); 
        dbService.findOneByID("1"); 
       } catch (Exception e) { 
        errors.add(e); 
       } finally { 
        doneSignal.countDown(); 
       } 
      } 
     }; 
     thread.start(); 
    } 
    // Create all the writer threads and start them 
    for (int i = 0; i < writer; i++) { 
     Thread thread = new Thread() { 
      public void run() { 
       try { 
        startSignal.await(); 
        dbService.save(entity.builder() 
         .setID("1").setName("John").build()); 
       } catch (Exception e) { 
        errors.add(e); 
       } finally { 
        doneSignal.countDown(); 
       } 
      } 
     }; 
     thread.start(); 
    } 
    // Create all the remover threads and start them 
    for (int i = 0; i < remover; i++) { 
     Thread thread = new Thread() { 
      public void run() { 
       try { 
        startSignal.await(); 
        dbService.delete("1"); 
       } catch (Exception e) { 
        errors.add(e); 
       } finally { 
        doneSignal.countDown(); 
       } 
      } 
     }; 
     thread.start(); 
    } 
    // Release the threads 
    startSignal.countDown(); 
    // Wait until all threads did their task 
    doneSignal.await(); 
    // If an error has been collected, print the stack trace and throws the 
    // first error to make the test fail 
    if (!errors.isEmpty()) { 
     for (Exception e : errors) { 
      e.printStackTrace(); 
     } 
     throw errors.get(0); 
    } 
} 

NB:あなたが与えられたユニットテストになりたい場合はいくつかの同時実行スレッドで実行されている場合は、contiperfを見てください。しかし、それを達成したい場合はそれらを混在させることはできません

+0

これはJUnitではなく面白い解決策です。それを使用している間、どのように私は各スレッド 'bookService'メソッドの結果がスレッドIDと結合されているかを調べることができますか? – mCs

+0

私はあなたの質問を理解していませんが、findOneByIDのようなメソッドの結果をテストしたい場合は、メソッドを呼び出した後にrunメソッドにアサーションを直接追加することができます。アサーションが失敗した場合は例外がスローされ、テストは失敗します。はい、私はそれが実際のユニットテストではないことを知っていますが、達成したいのは実際にユニットテストではありません –

+0

あなたはその方法について正しいと思います。はい、実際には単体テストのユースケースではありません。私はちょうどいくつかの 'aven-surefire-plugin'を見つけました。問題に答えるなら、私が投稿する同時テストを実行する可能性があります。あなたのソリューションをありがとう - もちろんupvoted。 – mCs

関連する問題