Skip to content

Commit c2b27a0

Browse files
deepin-wm18202781743
authored andcommitted
fix: fix serial comparison logic in DConfigCache setValue
1. Fix incorrect early return when checking value equality without serial comparison 2. Add DConfigInfo::checkSerial to compare both value and serial before returning false 3. Update setValue documentation to accurately describe return behavior 4. Add unit tests covering serial comparison scenarios: - Same value and same serial returns false - Same value but different serial returns true (bug fix) - Different value and same serial returns true - Different value and different serial returns true 5. Add test meta and override data files for serial testing 6. Update copyright year from 2023 to 2026 fix: 修复DConfigCache的setValue中序列号比较逻辑 1. 修正仅检查值相等就提前返回的错误逻辑 2. 增加DConfigInfo::checkSerial同时比较值和序列号 3. 更新setValue文档准确描述返回值行为 4. 添加涵盖序列号比较场景的单元测试: - 相同值和相同序列号返回false - 相同值但不同序列号返回true(bug修复) - 不同值相同序列号返回true - 不同值不同序列号返回true 5. 添加序列号测试所需的元数据和覆盖文件 6. 更新版权年份从2023到2026 Influence: 1. Test DConfig setValue when value is same but serial is updated via override 2. Verify no regression when setting different values with same or different serials 3. Validate cache update behavior matches documented return semantics 4. Ensure override layer serial changes properly trigger value updates Influence: 1. 测试当值相同但通过覆盖文件更新序列号时setValue的行为 2. 验证设置不同值(相同或不同序列号)时无回归 3. 确认缓存更新行为符合文档中的返回语义 4. 确保覆盖层序列号变化能正确触发值更新
1 parent 35a315f commit c2b27a0

5 files changed

Lines changed: 112 additions & 5 deletions

File tree

src/dconfigfile.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2021 - 2023 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2021 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -985,7 +985,7 @@ DConfigMetaImpl::~DConfigMetaImpl()
985985
\a value Configuration name
986986
\a uid User Id at setup time
987987
\a callerAppid Application id at setup time
988-
@return A value of true indicates that the new value has been reset, and false indicates that it has not been set
988+
@return true if the cache was updated (value or serial changed), false if no update was needed
989989
*/
990990

991991
/*!
@@ -1124,7 +1124,7 @@ class Q_DECL_HIDDEN DConfigCacheImpl : public DConfigCache {
11241124
}
11251125
bool setValue(const QString &key, const QVariant &value, const int serial, const uint uid, const QString &appid) override
11261126
{
1127-
if (values.value(key) == value) {
1127+
if (values.value(key) == value && DConfigInfo::checkSerial(values.serial(key), serial)) {
11281128
return false;
11291129
}
11301130
values.setValue(key, value);
@@ -1487,7 +1487,7 @@ QVariant DConfigFile::cacheValue(DConfigCache *userCache, const QString &key) co
14871487
\a value The value to set
14881488
\a userCache Specific user cache at setup time
14891489
\a appid Application id at setup time
1490-
@return A value of true indicates that the new value has been reset, and false indicates that it has not been set
1490+
@return true if the cache was updated (value or serial changed), false if no update was needed
14911491
*/
14921492
bool DConfigFile::setValue(const QString &key, const QVariant &value, const QString &callerAppid, DConfigCache *userCache)
14931493
{

tests/data.qrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<file>data/LGPLv3.txt</file>
1212
<file>data/example-license.json</file>
1313
<file>data/dconf-example.override.noexistitem.json</file>
14+
<file>data/dconf-serial-test.meta.json</file>
15+
<file>data/dconf-serial-test.override.json</file>
1416
<file>data/dconfig2cpp/basic-types.meta.json</file>
1517
<file>data/dconfig2cpp/numeric-types.meta.json</file>
1618
<file>data/dconfig2cpp/complex-types.meta.json</file>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"magic": "dsg.config.meta",
3+
"version": "1.0",
4+
"contents": {
5+
"testKey": {
6+
"value": "initial",
7+
"serial": 0,
8+
"flags": [],
9+
"name": "test key for serial comparison",
10+
"permissions": "readwrite",
11+
"visibility": "private"
12+
}
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"magic": "dsg.config.override",
3+
"version": "1.0",
4+
"contents": {
5+
"testKey": {
6+
"value": "initial",
7+
"serial": 1
8+
}
9+
}
10+
}

tests/ut_dconfigfile.cpp

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2021 - 2023 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2021 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -506,6 +506,87 @@ TEST_F(ut_DConfigFile, userPublic) {
506506
}
507507
}
508508

509+
TEST_F(ut_DConfigFile, setValueWithSerialComparison) {
510+
// 测试 DConfigCacheImpl::setValue 中的 serial 比较逻辑
511+
// 当值相同但 serial 不同时,应该返回 true(更新缓存)
512+
513+
FileCopyGuard guard(":/data/dconf-serial-test.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME));
514+
515+
// 场景1: 值和 serial 都相同 → 返回 false
516+
{
517+
DConfigFile config(APP_ID, FILE_NAME);
518+
ASSERT_TRUE(config.load(LocalPrefix));
519+
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid));
520+
ASSERT_TRUE(userCache->load(LocalPrefix));
521+
522+
// 首次设置值,serial=0 从 meta 传入
523+
ASSERT_TRUE(config.setValue("testKey", "test_value", "test", userCache.get()));
524+
525+
// 再次设置相同的值,serial 相同 → 返回 false
526+
ASSERT_FALSE(config.setValue("testKey", "test_value", "test", userCache.get()));
527+
}
528+
529+
// 场景2: 值相同,serial 不同 → 返回 true(新修复的场景)
530+
{
531+
DConfigFile config(APP_ID, FILE_NAME);
532+
ASSERT_TRUE(config.load(LocalPrefix));
533+
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid));
534+
ASSERT_TRUE(userCache->load(LocalPrefix));
535+
536+
// 设置值,缓存中 serial=0
537+
ASSERT_TRUE(config.setValue("testKey", "test_value", "test", userCache.get()));
538+
539+
// 使用 override 文件将 serial 升级到 1
540+
FileCopyGuard guard2(":/data/dconf-serial-test.override.json", QString("%1/%2.json").arg(overridePath, FILE_NAME));
541+
542+
// 重新加载配置,此时 meta serial=1
543+
DConfigFile config2(APP_ID, FILE_NAME);
544+
ASSERT_TRUE(config2.load(LocalPrefix));
545+
QScopedPointer<DConfigCache> userCache2(config2.createUserCache(uid));
546+
ASSERT_TRUE(userCache2->load(LocalPrefix));
547+
548+
// 设置相同的值,但 meta serial=1, cache serial=0 → 应该返回 true
549+
ASSERT_TRUE(config2.setValue("testKey", "test_value", "test", userCache2.get()));
550+
}
551+
552+
// 场景3: 值不同,serial 相同 → 返回 true
553+
{
554+
DConfigFile config(APP_ID, FILE_NAME);
555+
ASSERT_TRUE(config.load(LocalPrefix));
556+
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid));
557+
ASSERT_TRUE(userCache->load(LocalPrefix));
558+
559+
// 设置初始值
560+
ASSERT_TRUE(config.setValue("testKey", "value1", "test", userCache.get()));
561+
562+
// 设置不同的值,serial 相同 → 返回 true
563+
ASSERT_TRUE(config.setValue("testKey", "value2", "test", userCache.get()));
564+
}
565+
566+
// 场景4: 值不同,serial 不同 → 返回 true
567+
{
568+
DConfigFile config(APP_ID, FILE_NAME);
569+
ASSERT_TRUE(config.load(LocalPrefix));
570+
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid));
571+
ASSERT_TRUE(userCache->load(LocalPrefix));
572+
573+
// 设置初始值
574+
ASSERT_TRUE(config.setValue("testKey", "value1", "test", userCache.get()));
575+
576+
// 使用 override 文件将 serial 升级到 1
577+
FileCopyGuard guard2(":/data/dconf-serial-test.override.json", QString("%1/%2.json").arg(overridePath, FILE_NAME));
578+
579+
// 重新加载配置
580+
DConfigFile config2(APP_ID, FILE_NAME);
581+
ASSERT_TRUE(config2.load(LocalPrefix));
582+
QScopedPointer<DConfigCache> userCache2(config2.createUserCache(uid));
583+
ASSERT_TRUE(userCache2->load(LocalPrefix));
584+
585+
// 设置不同的值,serial 也不同 → 返回 true
586+
ASSERT_TRUE(config2.setValue("testKey", "value3", "test", userCache2.get()));
587+
}
588+
}
589+
509590
class ut_DConfigFileCheckName : public ut_DConfigFile, public ::testing::WithParamInterface<std::tuple<QString, bool>>
510591
{
511592

0 commit comments

Comments
 (0)