Skip to content

Conversation

@18202781743
Copy link
Contributor

Added comprehensive unit tests for the dconfig_org_deepin_dtk_preference
class generated by dconfig2cpp tool. The tests focus on crash stability
and thread safety of the generated DConfig wrapper class. The test
suite includes 8 test cases covering: immediate destruction during
initialization, destruction after successful initialization, race
conditions during initialization, signal thread affinity verification,
property change operations under stress, and concurrent config instance
handling. Tests check DBus service availability and skip execution if
DConfig service is not available. Added include path for log directory
in CMakeLists.txt to support test compilation.

Influence:

  1. Verify DConfig service is available on test system before running
    tests
  2. Test immediate destruction of config objects during initialization
  3. Test property getters and setters functionality
  4. Verify signal emission and thread affinity for property changes
  5. Test concurrent access with multiple config instances
  6. Validate rapid property changes don't cause crashes
  7. Check memory management during initialization race conditions

test: 为dconfig2cpp生成的类添加单元测试

为dconfig2cpp工具生成的dconfig_org_deepin_dtk_preference类添加了全面的
单元测试。测试重点在于生成的DConfig包装类的崩溃稳定性和线程安全性。测试
套件包含8个测试用例,涵盖:初始化期间的立即销毁、成功初始化后的销毁、初
始化期间的竞争条件、信号线程亲和性验证、压力下的属性更改操作以及并发配置
实例处理。测试会检查DBus服务可用性,如果DConfig服务不可用则跳过执行。在
CMakeLists.txt中添加了日志目录的包含路径以支持测试编译。

Influence:

  1. 验证测试系统上DConfig服务是否可用
  2. 测试初始化期间配置对象的立即销毁
  3. 测试属性getter和setter功能
  4. 验证属性更改时的信号发射和线程亲和性
  5. 测试多个配置实例的并发访问
  6. 验证快速属性更改不会导致崩溃
  7. 检查初始化竞争条件下的内存管理

Added comprehensive unit tests for the dconfig_org_deepin_dtk_preference
class generated by dconfig2cpp tool. The tests focus on crash stability
and thread safety of the generated DConfig wrapper class. The test
suite includes 8 test cases covering: immediate destruction during
initialization, destruction after successful initialization, race
conditions during initialization, signal thread affinity verification,
property change operations under stress, and concurrent config instance
handling. Tests check DBus service availability and skip execution if
DConfig service is not available. Added include path for log directory
in CMakeLists.txt to support test compilation.

Influence:
1. Verify DConfig service is available on test system before running
tests
2. Test immediate destruction of config objects during initialization
3. Test property getters and setters functionality
4. Verify signal emission and thread affinity for property changes
5. Test concurrent access with multiple config instances
6. Validate rapid property changes don't cause crashes
7. Check memory management during initialization race conditions

test: 为dconfig2cpp生成的类添加单元测试

为dconfig2cpp工具生成的dconfig_org_deepin_dtk_preference类添加了全面的
单元测试。测试重点在于生成的DConfig包装类的崩溃稳定性和线程安全性。测试
套件包含8个测试用例,涵盖:初始化期间的立即销毁、成功初始化后的销毁、初
始化期间的竞争条件、信号线程亲和性验证、压力下的属性更改操作以及并发配置
实例处理。测试会检查DBus服务可用性,如果DConfig服务不可用则跳过执行。在
CMakeLists.txt中添加了日志目录的包含路径以支持测试编译。

Influence:
1. 验证测试系统上DConfig服务是否可用
2. 测试初始化期间配置对象的立即销毁
3. 测试属性getter和setter功能
4. 验证属性更改时的信号发射和线程亲和性
5. 测试多个配置实例的并发访问
6. 验证快速属性更改不会导致崩溃
7. 检查初始化竞争条件下的内存管理
@deepin-ci-robot
Copy link
Contributor

deepin pr auto review

这段代码是一个针对 DConfig 生成的类的单元测试文件,主要测试了线程安全、崩溃稳定性和信号处理等方面。以下是对该代码的审查意见:

1. 语法逻辑

优点:

  • 代码结构清晰,测试用例划分合理,涵盖了初始化、销毁、属性变更和并发场景。
  • 使用了 GTEST_SKIP() 来处理依赖环境(DConfig 服务)不可用的情况,这是很好的实践。

改进建议:

  • 资源管理风险:多处使用裸指针 dconfig_org_deepin_dtk_preference * 并手动 delete。如果测试中途抛出异常(例如 ASSERT_* 失败),可能导致内存泄漏。建议使用 std::unique_ptrQScopedPointer 进行自动管理。

    // 修改前
    auto config = createConfig();
    // ... 测试代码 ...
    delete config;
    
    // 修改后
    std::unique_ptr<dconfig_org_deepin_dtk_preference> config(createConfig());
    // ... 测试代码 ...
    // 自动删除,无需手动 delete
  • 测试用例 4 (test_signal_thread_affinity):在 lambda 中捕获 signalThread 指针存在风险。如果信号从未触发,signalThread 将保持为 nullptr,后续的比较 EXPECT_EQ(signalThread, mainThread) 会失败但可能不会明确指出原因。建议在等待信号时也检查 signalThread 是否被设置。

  • 测试用例 6 (test_rapid_changes_then_destroy)QTest::qWait(1) 在循环中可能导致测试执行时间不可预测。考虑使用 QCoreApplication::processEvents() 替代,或者明确说明这是为了模拟真实场景的延迟。

2. 代码质量

优点:

  • 测试用例命名清晰,能够准确描述测试目的。
  • 使用了常量定义测试参数,便于维护和调整。

改进建议:

  • 魔法数字:虽然定义了常量,但 test_concurrent_configs 中硬编码了 3 个并发配置。建议将其提取为常量:

    static constexpr int CONCURRENT_CONFIG_COUNT = 3;
  • 注释:部分复杂逻辑(如 test_destroy_during_initialization 中的延迟计算)缺少注释,建议添加说明:

    // Calculate delay to hit different points in the initialization window
    // 0-900 microseconds, distributed across cycles
    int delayMicros = (cycle % 10) * 100;
  • 重复代码createConfig() 和初始化检查逻辑在多个测试用例中重复。可以提取为辅助函数:

    std::unique_ptr<dconfig_org_deepin_dtk_preference> createAndInitConfig()
    {
        auto config = std::unique_ptr<dconfig_org_deepin_dtk_preference>(createConfig());
        if (!QTest::qWaitFor([&config]() { return config->isInitializeSucceeded(); }, INIT_TIMEOUT_MS)) {
            return nullptr;
        }
        return config;
    }

3. 代码性能

优点:

  • 测试参数(如循环次数)已经针对快速执行进行了调整。

改进建议:

  • 测试用例 3 (test_destroy_during_initialization):使用 QThread::usleep 会阻塞线程,影响测试效率。可以考虑使用异步方式或更短的延迟。
  • 测试用例 7 (test_concurrent_configs):并发测试的配置数量较少(3个),可能无法充分暴露并发问题。建议增加配置数量或循环次数,但需要在性能和覆盖率之间权衡。

4. 代码安全

优点:

  • 检查了 DConfig 服务的可用性,避免在服务不可用时执行测试。

改进建议:

  • DBus 调用isDConfigServiceAvailable() 中的 DBus 调用没有设置超时,可能导致测试挂起。建议添加超时参数:

    QDBusReply<QStringList> activatableNames =
        QDBusConnection::systemBus().interface()->callWithArgumentList(
            QDBus::AutoDetect,
            QLatin1String("ListActivatableNames"),
            QList<QVariant>(),
            1000); // 1秒超时
  • 信号连接:在 test_signal_thread_affinity 中,使用 Qt::DirectConnection 连接信号到 lambda,这可能导致信号在非主线程中执行。建议验证这是否符合预期,或者使用 Qt::QueuedConnection 并在主线程中检查。

  • 属性变更:在 test_property_getters_and_setters 中,直接修改属性值(如 setAutoDisplayFeature)可能会影响系统配置。建议在测试后恢复原始值:

    bool originalValue = config->autoDisplayFeature();
    config->setAutoDisplayFeature(!originalValue);
    // ... 测试代码 ...
    config->setAutoDisplayFeature(originalValue); // 恢复原始值

5. 其他建议

  • 测试覆盖率:当前测试主要关注崩溃稳定性,建议增加对以下场景的测试:

    • 网络中断或 DBus 服务重启时的行为。
    • 属性值类型不匹配时的处理。
    • 大量并发配置实例的性能测试。
  • 日志输出:在关键步骤添加日志输出,便于调试:

    qDebug() << "Cycle" << cycle << ": Config created";
    qDebug() << "Cycle" << cycle << ": Config initialized:" << initialized;

总结

这段代码整体质量较高,测试覆盖了关键场景。主要改进方向是资源管理(使用智能指针)、减少重复代码、增加超时控制和日志输出,以及补充更多边界条件的测试。

@18202781743
Copy link
Contributor Author

@Dami-star

@deepin-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, zccrs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@zccrs zccrs merged commit 0e2cb8f into linuxdeepin:master Jan 22, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants