vector<ReplicaDescription> PlacementAndLoadBalancingUnitTest::CreateReplicas(wstring replicas, std::vector<bool>& isReplicaUp)
{
    StringCollection replicaSet;
    vector<ReplicaDescription> replicaDescription;
    StringUtility::Split<wstring>(replicas, replicaSet, ItemDelimiter);

    if (replicaSet.size() != isReplicaUp.size())
    {
        return replicaDescription;
    }

    for (int i = 0; i < replicaSet.size(); i++)
    {
        replicaDescription.push_back(CreateReplicas(replicaSet[i], isReplicaUp[i])[0]);
    }

    return replicaDescription;
}
map<wstring, uint> PlacementAndLoadBalancingUnitTest::CreateCapacities(std::wstring capacityStr)
{
    map<wstring, uint> capacities;
    if (!capacityStr.empty())
    {
        StringCollection capacityCollection;
        StringUtility::Split<wstring>(capacityStr, capacityCollection, ItemDelimiter);
        for (int i = 0; i < capacityCollection.size(); ++i)
        {
            StringCollection capacityPair;
            StringUtility::Split<wstring>(capacityCollection[i], capacityPair, PairDelimiter);

            ASSERT_IFNOT(capacityPair.size() == 2, "Capacity error");
            capacities.insert(make_pair(capacityPair[0], _wtoi(capacityPair[1].c_str())));
        }
    }

    return capacities;
}
bool UnreliableTransportConfiguration::AddSpecification(std::wstring const & name, std::wstring const & data)
{
    StringCollection params;
    StringUtility::Split<wstring>(data, params, L" ");

    wstring source = (params.size() > 0 ? params[0] : wstring());
    wstring destination = (params.size() > 1 ? params[1] : wstring());
    wstring action = (params.size() > 2 ? params[2] : wstring());
    double probability = (params.size() > 3 ? Common::Double_Parse(params[3]) : 1.0);
    TimeSpan delay = (params.size() > 4 ? ParseTimeSpan(params[4]) : TimeSpan::MaxValue);
    TimeSpan delaySpan = (params.size() > 5 ? ParseTimeSpan(params[5]) : TimeSpan::Zero);
    int priority = (params.size() > 6 ? Common::Int32_Parse(params[6]) : 0);
    int applyCount = (params.size() > 7 ? Common::Int32_Parse(params[7]) : -1);

    return AddSpecification(make_unique<UnreliableTransportSpecification>(name, source, destination, action, probability, delay, delaySpan, priority, applyCount));
}
Exemple #4
0
void TestDirectory::TestGetFilesWithMask()
{
	typedef std::vector<std::tstring> StringCollection;
	StringCollection fileNames = Workshare::System::IO::Directory::GetFiles(TEST_FOLDER + _T("\\TestGetFiles"), _T("*.doc"));
	assertEquals(2, (long)fileNames.size());

	// make all fileNames lowercase, so we can find them.
	std::for_each(fileNames.begin(), fileNames.end(), MakeFileNameLowerCase);

	StringCollection::iterator fileName = std::find(fileNames.begin(), fileNames.end(), tolower(TEST_FOLDER + _T("\\testgetfiles\\filea.doc")));
	assertMessage(fileName != fileNames.end(), _T("Expected to find [") + TEST_FOLDER + _T("\\testgetfiles\\filea.doc] in the returned list"));

	fileName = std::find(fileNames.begin(), fileNames.end(), tolower(TEST_FOLDER + _T("\\testgetfiles\\fileb.doc")));
	assertMessage(fileName != fileNames.end(), _T("Expected to find [") + TEST_FOLDER + _T("\\testgetfiles\\fileb.doc] in the returned list"));
}
bool TestMultiPackageHostContext::FromClientId(std::wstring const& clientId, TestMultiPackageHostContext & testMultiPackageHostContext)
{
    StringCollection collection;
    StringUtility::Split<wstring>(clientId, collection, TestMultiPackageHostContext::ParamDelimiter);
    if(collection.size() == 2)
    {
        Federation::NodeId nodeId;
        TestSession::FailTestIfNot(Federation::NodeId::TryParse(collection[0], nodeId), "Could not parse NodeId: {0}", clientId);
        testMultiPackageHostContext = TestMultiPackageHostContext(nodeId.ToString(), collection[1]);
        return true;
    }
    else
    {
        return false;
    }
}
vector<ServiceMetric> PlacementAndLoadBalancingUnitTest::CreateMetrics(wstring metrics)
{
    StringCollection metricCollection;
    StringUtility::Split<wstring>(metrics, metricCollection, ItemDelimiter);

    vector<ServiceMetric> metricList;
    for (wstring const& metricStr : metricCollection)
    {
        StringCollection metricProperties;
        StringUtility::Split<wstring>(metricStr, metricProperties, PairDelimiter);

        ASSERT_IFNOT(metricProperties.size() == 4, "Metric error");

        metricList.push_back(ServiceMetric(move(metricProperties[0]), Common::Double_Parse(metricProperties[1]), _wtoi(metricProperties[2].c_str()), _wtoi(metricProperties[3].c_str())));
    }

    return metricList;
}
map<wstring, ApplicationCapacitiesDescription> PlacementAndLoadBalancingUnitTest::CreateApplicationCapacities(wstring appCapacities)
{
    map<std::wstring, ApplicationCapacitiesDescription> capacityDesc;

    StringCollection capacityCollection;
    StringUtility::Split<wstring>(appCapacities, capacityCollection, ItemDelimiter);
    for (wstring const& metricStr : capacityCollection)
    {
        StringCollection metricProperties;
        StringUtility::Split<wstring>(metricStr, metricProperties, PairDelimiter);

        ASSERT_IFNOT(metricProperties.size() == 4, "CreateApplicationCapacities metric error");

        wstring metricName = metricProperties[0];
        capacityDesc.insert(make_pair(metricName,
            ApplicationCapacitiesDescription(move(metricProperties[0]), _wtoi(metricProperties[1].c_str()), _wtoi(metricProperties[2].c_str()), _wtoi(metricProperties[3].c_str()))));
    }

    return capacityDesc;
}
vector<ReplicaDescription> PlacementAndLoadBalancingUnitTest::CreateReplicas(wstring replicas, bool isUp)
{
    vector<ReplicaDescription> replicaVector;

    // format [replica1], [replica2], ...
    // each replica: [replicaRole]/[nodeIndex]/[IsToBePromoted][IsMoveInProgress][IsToBeDropped][IsInBuild]
    // replicaRole: P or S or SB
    // the last three descriptions optional inputs: PDB; treated as false by default
    StringCollection placement;
    StringUtility::Split<wstring>(replicas, placement, ItemDelimiter);

    for (wstring const& replica : placement)
    {
        StringCollection replicaFields;
        StringUtility::Split<wstring>(replica, replicaFields, PairDelimiter);

        ReplicaRole::Enum role = ReplicaRole::None;
        Reliability::ReplicaStates::Enum state = Reliability::ReplicaStates::Ready;
        StringUtility::TrimSpaces(replicaFields[0]);

        if (replicaFields[0] == L"P")
        {
            role = ReplicaRole::Primary;
        }
        else if (replicaFields[0] == L"S" || replicaFields[0] == L"I")
        {
            role = ReplicaRole::Secondary;
        }
        else if (replicaFields[0] == L"N")
        {
            role = ReplicaRole::None;
        }
        else if (replicaFields[0] == L"SB")
        {
            role = ReplicaRole::StandBy;
            state = Reliability::ReplicaStates::StandBy;
        }
        else if (replicaFields[0] == L"D")
        {
            role = ReplicaRole::Dropped;
            state = Reliability::ReplicaStates::Dropped;
        }

        int nodeIndex = _wtoi(replicaFields[1].c_str());
        Reliability::ReplicaFlags::Enum flags = Reliability::ReplicaFlags::None;

        if (replicaFields.size() >= 3)
        {
            wstring properties = replica.substr(replicaFields[0].length() + replicaFields[1].length() + 2);

            if (StringUtility::Contains<wstring>(properties, L"B"))
            {
                state = Reliability::ReplicaStates::InBuild;
            }

            if (StringUtility::Contains<wstring>(properties, L"I"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::PrimaryToBeSwappedOut);
            }

            if (StringUtility::Contains<wstring>(properties, L"J"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::PrimaryToBePlaced);
            }

            if (StringUtility::Contains<wstring>(properties, L"K"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::ReplicaToBePlaced);
            }

            if (StringUtility::Contains<wstring>(properties, L"V"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::MoveInProgress);
            }

            if (StringUtility::Contains<wstring>(properties, L"D"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::ToBeDroppedByFM);
            }

            if (StringUtility::Contains<wstring>(properties, L"R"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::ToBeDroppedByPLB);
            }

            if (StringUtility::Contains<wstring>(properties, L"T"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::ToBeDroppedForNodeDeactivation);
            }

            if (StringUtility::Contains<wstring>(properties, L"P"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::ToBePromoted);
            }

            if (StringUtility::Contains<wstring>(properties, L"N"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::PendingRemove);
            }

            if (StringUtility::Contains<wstring>(properties, L"Z"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::Deleted);
            }

            if (StringUtility::Contains<wstring>(properties, L"L"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::PreferredPrimaryLocation);
            }

            if (StringUtility::Contains<wstring>(properties, L"E"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::EndpointAvailable);
            }

            if (StringUtility::Contains<wstring>(properties, L"M"))
            {
                flags = static_cast<Reliability::ReplicaFlags::Enum>(flags ^ Reliability::ReplicaFlags::PreferredReplicaLocation);
            }
        }

        replicaVector.push_back(ReplicaDescription(
            CreateNodeInstance(nodeIndex, 0),
            role,
            state,
            isUp,   // isUp
            flags));
    }

    return replicaVector;
}