Merge pull request #9841 from obsidiansystems/float-speed-factor

Convert `Machine::speedFactor` from a non-neg int to a non-neg float

(cherry picked from commit 69d0ae27e376e7c7c4f237716b0149223b8a805a)
Change-Id: I2afb5cf9e4fe1384985c58353946135c3d102b42
This commit is contained in:
eldritch horrors 2024-03-04 08:40:38 +01:00
parent 044c117a9f
commit 2bb0a06346
3 changed files with 17 additions and 5 deletions

View file

@ -33,11 +33,14 @@ Machine::Machine(decltype(storeUri) storeUri,
systemTypes(systemTypes), systemTypes(systemTypes),
sshKey(sshKey), sshKey(sshKey),
maxJobs(maxJobs), maxJobs(maxJobs),
speedFactor(std::max(1U, speedFactor)), speedFactor(speedFactor == 0.0f ? 1.0f : std::move(speedFactor)),
supportedFeatures(supportedFeatures), supportedFeatures(supportedFeatures),
mandatoryFeatures(mandatoryFeatures), mandatoryFeatures(mandatoryFeatures),
sshPublicHostKey(sshPublicHostKey) sshPublicHostKey(sshPublicHostKey)
{} {
if (speedFactor < 0.0)
throw UsageError("speed factor must be >= 0");
}
bool Machine::systemSupported(const std::string & system) const bool Machine::systemSupported(const std::string & system) const
{ {
@ -136,6 +139,14 @@ static Machine parseBuilderLine(const std::string & line)
return result.value(); return result.value();
}; };
auto parseFloatField = [&](size_t fieldIndex) {
const auto result = string2Int<float>(tokens[fieldIndex]);
if (!result) {
throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'float'", fieldIndex, line);
}
return result.value();
};
auto ensureBase64 = [&](size_t fieldIndex) { auto ensureBase64 = [&](size_t fieldIndex) {
const auto & str = tokens[fieldIndex]; const auto & str = tokens[fieldIndex];
try { try {
@ -154,7 +165,7 @@ static Machine parseBuilderLine(const std::string & line)
isSet(1) ? tokenizeString<std::set<std::string>>(tokens[1], ",") : std::set<std::string>{settings.thisSystem}, isSet(1) ? tokenizeString<std::set<std::string>>(tokens[1], ",") : std::set<std::string>{settings.thisSystem},
isSet(2) ? tokens[2] : "", isSet(2) ? tokens[2] : "",
isSet(3) ? parseUnsignedIntField(3) : 1U, isSet(3) ? parseUnsignedIntField(3) : 1U,
isSet(4) ? parseUnsignedIntField(4) : 1U, isSet(4) ? parseFloatField(4) : 1.0f,
isSet(5) ? tokenizeString<std::set<std::string>>(tokens[5], ",") : std::set<std::string>{}, isSet(5) ? tokenizeString<std::set<std::string>>(tokens[5], ",") : std::set<std::string>{},
isSet(6) ? tokenizeString<std::set<std::string>>(tokens[6], ",") : std::set<std::string>{}, isSet(6) ? tokenizeString<std::set<std::string>>(tokens[6], ",") : std::set<std::string>{},
isSet(7) ? ensureBase64(7) : "" isSet(7) ? ensureBase64(7) : ""

View file

@ -13,7 +13,7 @@ struct Machine {
const std::set<std::string> systemTypes; const std::set<std::string> systemTypes;
const std::string sshKey; const std::string sshKey;
const unsigned int maxJobs; const unsigned int maxJobs;
const unsigned int speedFactor; const float speedFactor;
const std::set<std::string> supportedFeatures; const std::set<std::string> supportedFeatures;
const std::set<std::string> mandatoryFeatures; const std::set<std::string> mandatoryFeatures;
const std::string sshPublicHostKey; const std::string sshPublicHostKey;

View file

@ -12,6 +12,7 @@ using testing::SizeIs;
using nix::absPath; using nix::absPath;
using nix::FormatError; using nix::FormatError;
using nix::UsageError;
using nix::getMachines; using nix::getMachines;
using nix::Machine; using nix::Machine;
using nix::Machines; using nix::Machines;
@ -131,7 +132,7 @@ TEST(machines, getMachinesWithIncorrectFormat) {
settings.builders = "nix@scratchy.labs.cs.uu.nl - - 8 three"; settings.builders = "nix@scratchy.labs.cs.uu.nl - - 8 three";
EXPECT_THROW(getMachines(), FormatError); EXPECT_THROW(getMachines(), FormatError);
settings.builders = "nix@scratchy.labs.cs.uu.nl - - 8 -3"; settings.builders = "nix@scratchy.labs.cs.uu.nl - - 8 -3";
EXPECT_THROW(getMachines(), FormatError); EXPECT_THROW(getMachines(), UsageError);
settings.builders = "nix@scratchy.labs.cs.uu.nl - - 8 3 - - BAD_BASE64"; settings.builders = "nix@scratchy.labs.cs.uu.nl - - 8 3 - - BAD_BASE64";
EXPECT_THROW(getMachines(), FormatError); EXPECT_THROW(getMachines(), FormatError);
} }