gpt4 book ai didi

java - 使用JUnit @ResourceLock顺序执行一些测试类

转载 作者:行者123 更新时间:2023-12-02 03:02:16 26 4
gpt4 key购买 nike

我有一个大型集成测试套件,测试类是并行执行的,类方法是顺序执行的。有 2 个测试类使用相同的服务来创建/读取/删除实体,并且共享此服务会创建竞争条件:

  1. Foo 类正在创建一条记录
  2. Class Bar 正在删除所有记录
  3. Foo 类正在尝试读取创建的记录,断言它存在,但此时它已被 Bar 删除。

我尝试使用 JUnit 的 @ResourceLock 注释这两个类,但它不起作用,也许我遗漏了一些东西?

示例:

@ResourceLock("serivceResource")
class Foo {
@Test
void fooTest(){
//create a record
//read the created record
//assert record created
}
}



@ResourceLock("serivceResource")
class Bar {
@Test
void barTest(){
//wipe all records
}
}

Foo#fooTestBar#barTest@ResourceLock 的使用存在竞争条件争议。

我想知道我是否需要以某种方式配置 JUnit 才能使其工作?

最佳答案

我尝试重现并发现下面的测试在有或没有资源锁定的情况下都能按预期工作

@ResourceLock("serivceResource")
class Foo {
@BeforeAll static void beforeAll() { call("Foo.beforeAll");}
@BeforeEach void beforeEach() { call("Foo.beforeEach");}
@AfterAll static void afterAll() { call("Foo.afterAll");}
@AfterEach void afterEach() { call("Foo.afterEach");}
@Test void test1(){ call("Foo.test1");}
@Test void test2(){ call("Foo.test2");}
@Test void test3(){ call("Foo.test3");}

private static void call(String action) {
System.out.println("Enter "+action);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Leave "+action);
}
}

@ResourceLock("serivceResource")
class Bar {

@BeforeAll static void beforeAll() { call("Bar.beforeAll");}
@BeforeEach void beforeEach() { call("Bar.beforeEach");}
@AfterAll static void afterAll() { call("Bar.afterAll");}
@AfterEach void afterEach() { call("Bar.afterEach");}
@Test void test1(){ call("Bar.test1");}
@Test void test2(){ call("Bar.test2");}
@Test void test3(){ call("Bar.test3");}

private static void call(String action) {
System.out.println("Enter "+action);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Leave "+action);
}
}

如果设置了资源锁,则输出 - 一切都已序列化

Enter Bar.beforeAll
Leave Bar.beforeAll
Enter Bar.beforeEach
Leave Bar.beforeEach
Enter Bar.test1
Leave Bar.test1
Enter Bar.afterEach
Leave Bar.afterEach
Enter Bar.beforeEach
Leave Bar.beforeEach
Enter Bar.test2
Leave Bar.test2
Enter Bar.afterEach
Leave Bar.afterEach
Enter Bar.beforeEach
Leave Bar.beforeEach
Enter Bar.test3
Leave Bar.test3
Enter Bar.afterEach
Leave Bar.afterEach
Enter Bar.afterAll
Leave Bar.afterAll
Enter Foo.beforeAll
Leave Foo.beforeAll
Enter Foo.beforeEach
Leave Foo.beforeEach
Enter Foo.test1
Leave Foo.test1
Enter Foo.afterEach
Leave Foo.afterEach
Enter Foo.beforeEach
Leave Foo.beforeEach
Enter Foo.test2
Leave Foo.test2
Enter Foo.afterEach
Leave Foo.afterEach
Enter Foo.beforeEach
Leave Foo.beforeEach
Enter Foo.test3
Leave Foo.test3
Enter Foo.afterEach
Leave Foo.afterEach
Enter Foo.afterAll
Leave Foo.afterAll

如果未设置资源锁,则输出 - 一切都是并行的。 (不过,beforeAll/afterAll 看起来非常非常奇怪!)

Enter Bar.beforeEach
Enter Bar.beforeEach
Enter Bar.beforeEach
Enter Foo.beforeEach
Enter Foo.beforeEach
Enter Foo.beforeEach
Leave Foo.beforeEach
Leave Bar.beforeEach
Leave Foo.beforeEach
Leave Bar.beforeEach
Leave Foo.beforeEach
Leave Bar.beforeEach
Enter Bar.test3
Enter Foo.test1
Enter Bar.test1
Enter Foo.test3
Enter Foo.test2
Enter Bar.test2
Leave Bar.test3
Leave Bar.test1
Leave Foo.test3
Leave Foo.test1
Leave Foo.test2
Leave Bar.test2
Enter Bar.afterEach
Enter Bar.afterEach
Enter Foo.afterEach
Enter Foo.afterEach
Enter Bar.afterEach
Enter Foo.afterEach
Leave Bar.afterEach
Leave Foo.afterEach
Leave Foo.afterEach
Leave Foo.afterEach
Leave Bar.afterEach
Leave Bar.afterEach
Enter Foo.beforeAll
Enter Bar.beforeAll
Leave Foo.beforeAll
Leave Bar.beforeAll
Enter Bar.afterAll
Enter Foo.afterAll
Leave Bar.afterAll
Leave Foo.afterAll

我的猜测 - 您的 Foo 测试会干扰其他一些测试,而不是 Bar

关于java - 使用JUnit @ResourceLock顺序执行一些测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57044553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com