From 7992d6a6bfa0f460f8663c2d72d07b60755a4857 Mon Sep 17 00:00:00 2001 From: Vitaliy Batichko Date: Mon, 29 Nov 2010 20:42:28 +0200 Subject: Correct erl_global{register, unregister} functions C node needs DFLAG_DIST_MONITOR flag set when connecting, and support for processing monitoring start/end messages received from counterpart, to make global registration actually work. --- .../test/erl_global_SUITE_data/Makefile.first | 21 ++ .../test/erl_global_SUITE_data/Makefile.src | 41 ++++ .../test/erl_global_SUITE_data/erl_global_test.c | 263 +++++++++++++++++++++ 3 files changed, 325 insertions(+) create mode 100644 lib/erl_interface/test/erl_global_SUITE_data/Makefile.first create mode 100644 lib/erl_interface/test/erl_global_SUITE_data/Makefile.src create mode 100644 lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c (limited to 'lib/erl_interface/test/erl_global_SUITE_data') diff --git a/lib/erl_interface/test/erl_global_SUITE_data/Makefile.first b/lib/erl_interface/test/erl_global_SUITE_data/Makefile.first new file mode 100644 index 0000000000..8e3fcb924e --- /dev/null +++ b/lib/erl_interface/test/erl_global_SUITE_data/Makefile.first @@ -0,0 +1,21 @@ +# +# %CopyrightBegin% +# +# Copyright Ericsson AB 2001-2010. All Rights Reserved. +# +# The contents of this file are subject to the Erlang Public License, +# Version 1.1, (the "License"); you may not use this file except in +# compliance with the License. You should have received a copy of the +# Erlang Public License along with this software. If not, it can be +# retrieved online at http://www.erlang.org/. +# +# Software distributed under the License is distributed on an "AS IS" +# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +# the License for the specific language governing rights and limitations +# under the License. +# +# %CopyrightEnd% +# + +erl_global_test_decl.c: erl_global_test.c + erl -noinput -pa ../all_SUITE_data -s init_tc run erl_global_test -s erlang halt diff --git a/lib/erl_interface/test/erl_global_SUITE_data/Makefile.src b/lib/erl_interface/test/erl_global_SUITE_data/Makefile.src new file mode 100644 index 0000000000..ef846bc440 --- /dev/null +++ b/lib/erl_interface/test/erl_global_SUITE_data/Makefile.src @@ -0,0 +1,41 @@ +# +# %CopyrightBegin% +# +# Copyright Ericsson AB 2000-2010. All Rights Reserved. +# +# The contents of this file are subject to the Erlang Public License, +# Version 1.1, (the "License"); you may not use this file except in +# compliance with the License. You should have received a copy of the +# Erlang Public License along with this software. If not, it can be +# retrieved online at http://www.erlang.org/. +# +# Software distributed under the License is distributed on an "AS IS" +# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +# the License for the specific language governing rights and limitations +# under the License. +# +# %CopyrightEnd% +# + +include @erl_interface_mk_include@@DS@eidefs.mk + +CC0 = @CC@ +CC = ..@DS@all_SUITE_data@DS@gccifier@exe@ -CC"$(CC0)" +LD = @LD@ +LIBPATH = @erl_interface_libpath@ +LIBERL = $(LIBPATH)/@erl_interface_lib@ +LIBEI = $(LIBPATH)/@erl_interface_eilib@ +LIBFLAGS = ../all_SUITE_data/runner@obj@ \ + $(LIBERL) $(LIBEI) @LIBS@ @erl_interface_sock_libs@ \ + @erl_interface_threadlib@ +CFLAGS = @EI_CFLAGS@ $(THR_DEFS) -I@erl_interface_include@ -I../all_SUITE_data +OBJS = erl_global_test@obj@ erl_global_test_decl@obj@ + +all: erl_global_test@exe@ + +erl_global_test@exe@: $(OBJS) $(LIBERL) $(LIBEI) + $(LD) @CROSSLDFLAGS@ -o $@ $(OBJS) $(LIBFLAGS) + +clean: + $(RM) $(OBJS) + $(RM) erl_global_test@exe@ diff --git a/lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c b/lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c new file mode 100644 index 0000000000..fc698ca0c4 --- /dev/null +++ b/lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c @@ -0,0 +1,263 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2000-2010. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +/* + * Purpose: Tests the functions in erl_global.c. + * + * See the erl_global_SUITE.erl file for a "table of contents". + */ + +#include +#include +#include + +#include "runner.h" + +static void cmd_erl_connect(ETERM* args); +static void cmd_erl_global_register(ETERM *args); +static void cmd_erl_global_whereis(ETERM *args); +static void cmd_erl_global_names(ETERM *args); +static void cmd_erl_global_unregister(ETERM *args); +static void cmd_erl_close_connection(ETERM *args); + +static void send_errno_result(int value); + +static struct { + char* name; + int num_args; /* Number of arguments. */ + void (*func)(ETERM* args); +} commands[] = { + "erl_connect", 4, cmd_erl_connect, + "erl_close_connection", 1, cmd_erl_close_connection, + "erl_global_register", 2, cmd_erl_global_register, + "erl_global_whereis", 1, cmd_erl_global_whereis, + "erl_global_names", 0, cmd_erl_global_names, + "erl_global_unregister", 2, cmd_erl_global_unregister, +}; + + +/* + * Sends a list contaning all data types to the Erlang side. + */ + +TESTCASE(interpret) +{ + ETERM* term; + + erl_init(NULL, 0); + + outer_loop: + + term = get_term(); + + if (term == NULL) { + report(1); + return; + } else { + ETERM* Func; + ETERM* Args; + int i; + + if (!ERL_IS_TUPLE(term) || ERL_TUPLE_SIZE(term) != 2) { + fail("term should be a tuple of size 2"); + } + + Func = erl_element(1, term); + if (!ERL_IS_ATOM(Func)) { + fail("function name should be an atom"); + } + Args = erl_element(2, term); + if (!ERL_IS_TUPLE(Args)) { + fail("function arguments should be a tuple"); + } + erl_free_term(term); + for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) { + int n = strlen(commands[i].name); + if (ERL_ATOM_SIZE(Func) != n) { + continue; + } + if (memcmp(ERL_ATOM_PTR(Func), commands[i].name, n) == 0) { + erl_free_term(Func); + if (ERL_TUPLE_SIZE(Args) != commands[i].num_args) { + fail("wrong number of arguments"); + } + commands[i].func(Args); + erl_free_term(Args); + goto outer_loop; + } + } + fail("bad command"); + } +} + +#define VERIFY_TYPE(Test, Term) \ +if (!Test(Term)) { \ + fail("wrong type for " #Term); \ +} else { \ +} + +static void +cmd_erl_connect(ETERM* args) +{ + ETERM* number; + ETERM* node; + ETERM* cookie; + + int res; + char buffer[256]; + + number = ERL_TUPLE_ELEMENT(args, 0); + VERIFY_TYPE(ERL_IS_INTEGER, number); + node = ERL_TUPLE_ELEMENT(args, 1); + VERIFY_TYPE(ERL_IS_ATOM, node); + cookie = ERL_TUPLE_ELEMENT(args, 2); + VERIFY_TYPE(ERL_IS_ATOM, cookie); + + if (ERL_ATOM_SIZE(cookie) == 0) { + res = erl_connect_init(ERL_INT_VALUE(number), 0, 0); + } else { + memcpy(buffer, ERL_ATOM_PTR(cookie), ERL_ATOM_SIZE(cookie)); + buffer[ERL_ATOM_SIZE(cookie)] = '\0'; + res = erl_connect_init(ERL_INT_VALUE(number), buffer, 0); + } + + if(!res) { + send_errno_result(res); + return; + } + + memcpy(buffer, ERL_ATOM_PTR(node), ERL_ATOM_SIZE(node)); + buffer[ERL_ATOM_SIZE(node)] = '\0'; + send_errno_result(erl_connect(buffer)); +} + +static void +cmd_erl_close_connection(ETERM* args) +{ + ETERM* number; + ETERM* res; + + number = ERL_TUPLE_ELEMENT(args, 0); + VERIFY_TYPE(ERL_IS_INTEGER, number); + res = erl_mk_int(erl_close_connection(ERL_INT_VALUE(number))); + send_term(res); + erl_free_term(res); +} + +static void +cmd_erl_global_register(ETERM* args) +{ + ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0); + ETERM* name = ERL_TUPLE_ELEMENT(args, 1); + ETERM* pid = erl_mk_pid(erl_thisnodename(), 14, 0, 0); + + char buffer[256]; + + VERIFY_TYPE(ERL_IS_INTEGER, fd_term); + VERIFY_TYPE(ERL_IS_ATOM, name); + + memcpy(buffer, ERL_ATOM_PTR(name), ERL_ATOM_SIZE(name)); + buffer[ERL_ATOM_SIZE(name)] = '\0'; + + send_errno_result(erl_global_register(ERL_INT_VALUE(fd_term), buffer, pid)); + erl_free_term(pid); +} + +static void +cmd_erl_global_whereis(ETERM* args) +{ + ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0); + ETERM* name = ERL_TUPLE_ELEMENT(args, 1); + ETERM* pid = NULL; + + char buffer[256]; + + VERIFY_TYPE(ERL_IS_INTEGER, fd_term); + VERIFY_TYPE(ERL_IS_ATOM, name); + + memcpy(buffer, ERL_ATOM_PTR(name), ERL_ATOM_SIZE(name)); + buffer[ERL_ATOM_SIZE(name)] = '\0'; + + pid = erl_global_whereis(ERL_INT_VALUE(fd_term), buffer, NULL); + send_term(pid); + erl_free_term(pid); +} + +static void +cmd_erl_global_names(ETERM* args) +{ + ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0); + + ETERM* res_array[2], *res_tuple, *name; + char** names = NULL; + int count = 0, i; + + VERIFY_TYPE(ERL_IS_INTEGER, fd_term); + + names = erl_global_names(ERL_INT_VALUE(fd_term), &count); + + res_array[0] = erl_mk_empty_list(); + for(i=0; i Date: Thu, 30 Dec 2010 17:48:36 +0100 Subject: Fix testcases erl_global_whereis, erl_global_names Enable erl_global_whereis and erl_global names. --- lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/erl_interface/test/erl_global_SUITE_data') diff --git a/lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c b/lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c index fc698ca0c4..dc0d8a0091 100644 --- a/lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c +++ b/lib/erl_interface/test/erl_global_SUITE_data/erl_global_test.c @@ -46,8 +46,8 @@ static struct { "erl_connect", 4, cmd_erl_connect, "erl_close_connection", 1, cmd_erl_close_connection, "erl_global_register", 2, cmd_erl_global_register, - "erl_global_whereis", 1, cmd_erl_global_whereis, - "erl_global_names", 0, cmd_erl_global_names, + "erl_global_whereis", 2, cmd_erl_global_whereis, + "erl_global_names", 1, cmd_erl_global_names, "erl_global_unregister", 2, cmd_erl_global_unregister, }; -- cgit v1.2.3