Skip to content

Commit

Permalink
Merge pull request #418 from cmoussa1/move.split_string
Browse files Browse the repository at this point in the history
plugin: move `split_string ()` out of plugin code
  • Loading branch information
mergify[bot] authored Feb 16, 2024
2 parents b291ffb + 8c2267b commit 72b0c1a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/plugins/accounting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,18 @@ json_t* convert_map_to_json (std::map<int, std::map<std::string, Association>>

return accounting_data;
}


void split_string_and_push_back (const char *list,
std::vector<std::string> &vec)
{
std::stringstream s_stream;

s_stream << list; // create string stream from string

while (s_stream.good ()) {
std::string substr;
getline (s_stream, substr, ','); // get string delimited by comma
vec.push_back (substr);
}
}
5 changes: 5 additions & 0 deletions src/plugins/accounting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#include <string>
#include <map>
#include <iterator>
#include <sstream>

// all attributes are per-user/bank
class Association {
Expand Down Expand Up @@ -57,4 +58,8 @@ Association* get_association (int userid,
json_t* convert_map_to_json (std::map<int, std::map<std::string, Association>>
&users);

// split a list of items and add them to a vector in an Association object
void split_string_and_push_back (const char *list,
std::vector<std::string> &vec);

#endif // ACCOUNTING_H
6 changes: 3 additions & 3 deletions src/plugins/mf_priority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ static void rec_update_cb (flux_t *h,
const flux_msg_t *msg,
void *arg)
{
char *bank, *def_bank, *queues = NULL;
char *bank, *def_bank, *assoc_queues = NULL;
int uid, max_running_jobs, max_active_jobs = 0;
double fshare = 0.0;
json_t *data, *jtemp = NULL;
Expand Down Expand Up @@ -360,7 +360,7 @@ static void rec_update_cb (flux_t *h,
"fairshare", &fshare,
"max_running_jobs", &max_running_jobs,
"max_active_jobs", &max_active_jobs,
"queues", &queues,
"queues", &assoc_queues,
"active", &active) < 0)
flux_log (h, LOG_ERR, "mf_priority unpack: %s", error.text);

Expand All @@ -375,7 +375,7 @@ static void rec_update_cb (flux_t *h,

// split queues comma-delimited string and add it to b->queues vector
b->queues.clear ();
split_string (queues, b);
split_string_and_push_back (assoc_queues, b->queues);

users_def_bank[uid] = def_bank;
}
Expand Down
15 changes: 14 additions & 1 deletion src/plugins/test/accounting_test01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,22 @@ static void test_get_association_no_default_bank ()
}


// ensure split_string_and_push_back () works with a list of items
static void split_string_and_push_back_success ()
{
const char *assoc_queues = "bronze,silver,gold";
std::vector<std::string> expected_queues = {"bronze", "silver", "gold"};

split_string_and_push_back (assoc_queues, users[1001]["bank_A"].queues);
ok (users[1001]["bank_A"].queues == expected_queues,
"split_string_and_push_back () works");
}


int main (int argc, char* argv[])
{
// declare the number of tests that we plan to run
plan (4);
plan (5);

// add users to the test map
initialize_map (users);
Expand All @@ -125,6 +137,7 @@ int main (int argc, char* argv[])
test_get_association_success ();
test_get_association_noexist ();
test_get_association_no_default_bank ();
split_string_and_push_back_success ();

// indicate we are done testing
done_testing ();
Expand Down

0 comments on commit 72b0c1a

Please sign in to comment.