Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): Improve the robustness of testing standards. #674

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void revert(String namespace, InstanceId instanceId, MachineState machine
revertFilter(namespace, instanceId, machineState),
revertUpdate(instanceId, machineState)
);
if (updateResult.getModifiedCount() == 0) {
if (updateResult.getMatchedCount() == 0) {
throw new MachineIdLostException(namespace, instanceId, machineState);
}
}
Expand All @@ -138,7 +138,7 @@ public void guard(String namespace, InstanceId instanceId, MachineState machineS
guardFilter(namespace, instanceId, machineState),
guardUpdate(machineState.getLastTimeStamp())
);
if (updateResult.getModifiedCount() == 0) {
if (updateResult.getMatchedCount() == 0) {
throw new MachineIdLostException(namespace, instanceId, machineState);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import me.ahoo.cosid.test.MockIdGenerator;
import me.ahoo.cosid.test.TestSpec;

import com.google.common.base.Stopwatch;
import lombok.SneakyThrows;
import org.slf4j.Logger;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.locks.LockSupport;
import java.util.function.Supplier;

/**
Expand All @@ -37,52 +40,62 @@
* @author ahoo wang
*/
public class DistributeSafeGuard implements TestSpec {


private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DistributeSafeGuard.class);
private final Supplier<MachineIdDistributor> implFactory;
private final int machineBit;
private final Duration safeGuardDuration;

public DistributeSafeGuard(Supplier<MachineIdDistributor> implFactory, int machineBit, Duration safeGuardDuration) {
this.implFactory = implFactory;
this.machineBit = machineBit;
this.safeGuardDuration = safeGuardDuration;
}


@SneakyThrows
@Override
public void verify() {
MachineIdDistributor distributor = implFactory.get();

String namespace = MockIdGenerator.usePrefix("DistributeSafeGuard").generateAsString();
logger.info("Verify namespace:{}", namespace);

int moreMachineBit = machineBit + 1;
List<InstanceId> allInstances = allInstances(moreMachineBit, false);
int endIdx = MachineIdDistributor.totalMachineIds(machineBit);
List<InstanceId> availableInstances = allInstances.subList(0, endIdx);
assertThat(availableInstances, hasSize(MachineIdDistributor.totalMachineIds(machineBit)));

for (int i = 0; i < availableInstances.size(); i++) {
int machineId = distributor.distribute(namespace, machineBit, allInstances.get(i), safeGuardDuration).getMachineId();
assertThat(machineId, equalTo(i));
}

InstanceId overflowInstanceId = mockInstance(MachineIdDistributor.totalMachineIds(machineBit), false);
Assert.assertThrows(MachineIdOverflowException.class, () -> {
distributor.distribute(namespace, machineBit, overflowInstanceId, safeGuardDuration);
});

/*
* 等待所有实例到达安全守护点(SafeGuardAt),即变成可回收状态.
*/
LockSupport.parkNanos(this, safeGuardDuration.plusMillis(300).toNanos());
Stopwatch stopwatch = Stopwatch.createStarted();
Thread.sleep(safeGuardDuration.plusMillis(300).toMillis());
logger.info("Verify namespace:{},safeGuardDuration:[{}], SafeGuardAt:[{}] ,Time:[{}]", namespace,
safeGuardDuration,
MachineIdDistributor.getSafeGuardAt(safeGuardDuration, false),
stopwatch.elapsed()
);
availableInstances = allInstances.subList(endIdx, MachineIdDistributor.totalMachineIds(moreMachineBit));

Integer[] machineIds = availableInstances
.stream()
.map(instanceId -> distributor.distribute(namespace, machineBit, instanceId, safeGuardDuration).getMachineId())
.sorted().toArray(Integer[]::new);
.stream()
.map(instanceId -> distributor.distribute(namespace, machineBit, instanceId, safeGuardDuration).getMachineId())
.sorted().toArray(Integer[]::new);

for (int i = 0; i < machineIds.length; i++) {
assertThat(machineIds[i], equalTo(i));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import me.ahoo.cosid.test.MockIdGenerator;
import me.ahoo.cosid.test.TestSpec;

import lombok.SneakyThrows;

import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;

Expand All @@ -33,12 +35,13 @@
public class Guard implements TestSpec {
private final Supplier<MachineIdDistributor> implFactory;
private final int machineBit;

public Guard(Supplier<MachineIdDistributor> implFactory, int machineBit) {
this.implFactory = implFactory;
this.machineBit = machineBit;
}


@SneakyThrows
@Override
public void verify() {
MachineIdDistributor distributor = implFactory.get();
Expand Down
Loading