Skip to content
Open
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
10 changes: 7 additions & 3 deletions src/backend/commands/resgroupcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1632,17 +1632,21 @@ getCpuSetByRole(const char *cpuset)
splitcpuset = (char *)cpuset;
else
{
char *scpu = first + 1;
char *second = first + 1;

/* Get result cpuset by IS_QUERY_DISPATCHER(), on master or segment */
if (IS_QUERY_DISPATCHER())
splitcpuset = scpu;
else
{
char *mcpu = (char *)palloc0(sizeof(char) * MaxCpuSetLength);
strncpy(mcpu, cpuset, first - cpuset);
splitcpuset = mcpu;
}
else
{
char *scpu = (char *)palloc0(sizeof(char) * MaxCpuSetLength);
strlcpy(scpu, second, MaxCpuSetLength);
splitcpuset = scpu;
}
}

return splitcpuset;
Expand Down
5 changes: 4 additions & 1 deletion src/backend/commands/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ subdir=src/backend/commands
top_builddir=../../../..
include $(top_builddir)/src/Makefile.global

TARGETS=tablecmds
TARGETS=tablecmds resgroupcmds

include $(top_srcdir)/src/backend/mock.mk

tablecmds.t: \
$(MOCK_DIR)/backend/access/aocs/aocs_compaction_mock.o \
$(MOCK_DIR)/backend/access/hash/hash_mock.o \
$(MOCK_DIR)/backend/utils/fmgr/fmgr_mock.o

resgroupcmds.t: \
$(MOCK_DIR)/backend/utils/fmgr/fmgr_mock.o
135 changes: 135 additions & 0 deletions src/backend/commands/test/resgroupcmds_test.c
Comment thread
nix-oss marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*-------------------------------------------------------------------------
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* resgroupcmds_test.c
*
* IDENTIFICATION
* src/backend/commands/test/resgroupcmds_test.c
*
*-------------------------------------------------------------------------
*/

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include "cmockery.h"

#include "../resgroupcmds.c"

/*
* Helper: simulate running on the coordinator (dispatcher).
*/
static void
set_role_coordinator(void)
{
GpIdentity.segindex = MASTER_CONTENT_ID;
}

/*
* Helper: simulate running on a segment.
*/
static void
set_role_segment(void)
{
GpIdentity.segindex = 0;
}

/*
* NULL input must raise an ERROR.
*/
static void
test__getCpuSetByRole_null_input(void **state)
{
PG_TRY();
{
getCpuSetByRole(NULL);
fail_msg("expected ereport(ERROR) for NULL cpuset");
}
PG_CATCH();
{
FlushErrorState();
}
PG_END_TRY();
}

/*
* cpuset without a separator: same value must be returned
* regardless of the role.
*/
static void
test__getCpuSetByRole_no_separator(void **state)
{
const char *input = "0-7";
char *result;

set_role_coordinator();
result = getCpuSetByRole(input);
assert_string_equal(result, "0-7");

set_role_segment();
result = getCpuSetByRole(input);
assert_string_equal(result, "0-7");
}

/*
* cpuset with a separator, called as coordinator:
* must return the part BEFORE the ';'.
*/
static void
test__getCpuSetByRole_with_separator_as_coordinator(void **state)
{
const char *input = "0-7;0-15";
char *result;

set_role_coordinator();
result = getCpuSetByRole(input);
assert_string_equal(result, "0-7");
}

/*
* cpuset with a separator, called as segment:
* must return the part AFTER the ';'.
*/
static void
test__getCpuSetByRole_with_separator_as_segment(void **state)
{
const char *input = "0-7;0-15";
char *result;

set_role_segment();
result = getCpuSetByRole(input);
assert_string_equal(result, "0-15");
}

int
main(int argc, char *argv[])
{
cmockery_parse_arguments(argc, argv);

const UnitTest tests[] = {
unit_test(test__getCpuSetByRole_null_input),
unit_test(test__getCpuSetByRole_no_separator),
unit_test(test__getCpuSetByRole_with_separator_as_coordinator),
unit_test(test__getCpuSetByRole_with_separator_as_segment)
};

MemoryContextInit();

return run_tests(tests);
}
Loading