Skip to content

Commit

Permalink
WIP: testsuite module
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Vountesmery committed Nov 12, 2024
1 parent 2e5bf9f commit d8c3de6
Show file tree
Hide file tree
Showing 14 changed files with 710 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ clean-extern:
proper-extern:
$(MAKE) proper -C extern/

.PHONY: all extern mime lib clean $(TESTBENCH) testbench runtest $(DTESTS) $(RUNTESTS)
.PHONY: all extern mime lib clean $(BINTESTBENCH) testbench runtest $(DTESTS) $(RUNTESTS)
10 changes: 9 additions & 1 deletion nngd/nngtests/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ public {
import nngd.nngtests.test02;
import nngd.nngtests.test03;
import nngd.nngtests.test04;
import nngd.nngtests.test05;
import nngd.nngtests.test06;
import nngd.nngtests.test07;
import nngd.nngtests.test08;

static immutable string[] testlist = (){
return [
nngd.nngtests.test00._testclass,
nngd.nngtests.test01._testclass,
nngd.nngtests.test02._testclass,
nngd.nngtests.test03._testclass,
nngd.nngtests.test04._testclass
nngd.nngtests.test04._testclass,
nngd.nngtests.test05._testclass,
nngd.nngtests.test06._testclass,
nngd.nngtests.test07._testclass,
nngd.nngtests.test08._testclass
];
}();

Expand Down
3 changes: 2 additions & 1 deletion nngd/nngtests/test00.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import nngd;
const _testclass = "nngd.nngtests.nng_test00_template";


class nng_test00_template : NNGTest {
@trusted class nng_test00_template : NNGTest {

this(Args...)(auto ref Args args) { super(args); }

override string[] run(){
log("NNG test template");
log(_testclass ~ ": Bye!");
return [];
}

Expand Down
3 changes: 2 additions & 1 deletion nngd/nngtests/test01.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import nngd;

const _testclass = "nngd.nngtests.nng_test01_pushpull_buffer";

class nng_test01_pushpull_buffer : NNGTest {
@trusted class nng_test01_pushpull_buffer : NNGTest {

this(Args...)(auto ref Args args) { super(args); }

Expand All @@ -22,6 +22,7 @@ class nng_test01_pushpull_buffer : NNGTest {
workers ~= new Thread(&(this.sender_worker)).start();
foreach(w; workers)
w.join();
log(_testclass ~ ": Bye!");
return [];
}

Expand Down
3 changes: 2 additions & 1 deletion nngd/nngtests/test02.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import nngd;

const _testclass = "nngd.nngtests.nng_test02_pushpull_string";

class nng_test02_pushpull_string : NNGTest {
@trusted class nng_test02_pushpull_string : NNGTest {

this(Args...)(auto ref Args args) { super(args); }

Expand All @@ -23,6 +23,7 @@ class nng_test02_pushpull_string : NNGTest {
workers ~= new Thread(&(this.sender_worker)).start();
foreach(w; workers)
w.join();
log(_testclass ~ ": Bye!");
return [];
}

Expand Down
4 changes: 3 additions & 1 deletion nngd/nngtests/test03.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import nngd;

const _testclass = "nngd.nngtests.nng_test03_pushpull_transport";

class nng_test03_pushpull_transport : NNGTest {
@trusted class nng_test03_pushpull_transport : NNGTest {

this(Args...)(auto ref Args args) { super(args); }

Expand All @@ -26,6 +26,8 @@ class nng_test03_pushpull_transport : NNGTest {
foreach(w; workers)
w.join();
}
log("BB");
log(_testclass ~ ": Bye!");
return [];
}

Expand Down
3 changes: 2 additions & 1 deletion nngd/nngtests/test04.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import nngd;

const _testclass = "nngd.nngtests.nng_test04_pubsub";

class nng_test04_pubsub : NNGTest {
@trusted class nng_test04_pubsub : NNGTest {

this(Args...)(auto ref Args args) { super(args); }

Expand All @@ -29,6 +29,7 @@ class nng_test04_pubsub : NNGTest {
}
foreach(w; workers)
w.join();
log(_testclass ~ ": Bye!");
return [];
}

Expand Down
144 changes: 144 additions & 0 deletions nngd/nngtests/test05.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
module nngd.nngtests.test05;

import std.stdio;
import std.concurrency;
import std.exception;
import std.json;
import std.format;
import std.conv;
import std.regex;
import core.thread;
import core.thread.osthread;
import nngd;

const _testclass = "nngd.nngtests.nng_test05_reqrep";

@trusted class nng_test05_reqrep : NNGTest {

this(Args...)(auto ref Args args) { super(args); }

override string[] run(){
log("NNG test 05: request-reply");
this.uri = "tcp://127.0.0.1:31005";
immutable string[] tags = ["TAG0", "TAG1", "TAG2", "TAG3"];
workers ~= new Thread(&(this.server_worker)).start();
foreach(t; tags){
this.tag = t;
workers ~= new Thread(&(this.client_worker)).start();
}
foreach(w; workers)
w.join();
log(_testclass ~ ": Bye!");
return [];
}

void server_worker() @trusted {
const NTAGS = 4;
const NMSGS = 31;
auto ctr = regex(r" ([0-9]+)$");
uint k = 0, p=0;
int rc;
try{
thread_attachThis();
rt_moduleTlsCtor();
log("REP: Listening at " ~ uri);
NNGSocket s = NNGSocket(nng_socket_type.NNG_SOCKET_REP);
s.sendtimeout = msecs(1000);
s.recvtimeout = msecs(1000);
s.sendbuf = 4096;
rc = s.listen(uri);
enforce(rc == 0);
while( p < NTAGS ){
auto line = s.receive!string();
if(s.errno != 0){
error("REP: RECV ERROR: " ~ nng_errstr(s.errno));
continue;
}
k++;
log("REP: RECV: " ~ line);
auto rres = matchFirst(line, ctr);
line = format("REPLY(%d) = %s",k,line);
if(!rres.empty){
auto i = to!int(rres[1]);
if(i>NMSGS){
line = "END";
p++;
}
}
rc = s.send!string(line);
if(rc != 0){
error("REP: SEND ERROR: " ~ nng_errstr(rc));
}else{
log("REP: SENT: " ~ line);
}
}
log("REP: bye!");
} catch(Throwable e) {
error(dump_exception_recursive(e, "SS: Sender worker"));
}
}

void client_worker() @trusted {
const NDIALS = 32;
uint k = 0;
int rc;
bool _ok = false;
string tag = this.tag.dup;
Thread.sleep(msecs(10));
try{
thread_attachThis();
rt_moduleTlsCtor();
NNGSocket s = NNGSocket(nng_socket_type.NNG_SOCKET_REQ);
s.recvtimeout = msecs(1000);
while(++k < NDIALS){
log("REQ("~tag~"): to dial...");
rc = s.dial(uri);
if(rc == 0)
break;
if(rc == nng_errno.NNG_ECONNREFUSED){
nng_sleep(msecs(100));
continue;
}
error("REQ("~tag~"): Dial error: ",nng_errstr(rc));
enforce(rc == 0);
}
if(s.state is nng_socket_state.NNG_STATE_CONNECTED){
log("REQ: connected with : " ~ nng_errstr(s.errno));
}else{
enforce(false, "SS: connection timed out");
}
k = 0;
while(true){
k++;
auto line = format("Client(%s) request %d", tag, k);
rc = s.send!string(line);
enforce(rc == 0);
log("REQ("~tag~"): SENT: " ~ line);
auto str = s.receive!string();
if(s.errno == 0){
log(format("REQ("~tag~") RECV [%03d]: %s", str.length, str));
}else{
error("REQ("~tag~"): Error string: " ~ nng_errstr(s.errno));
}
if(str == "END"){
_ok = true;
break;
}
}
if(!_ok){
error("Test stopped without normal end.");
}
log("REQ("~tag~"): bye!");
} catch(Throwable e) {
error(dump_exception_recursive(e, "RR: Receiver worker"));
}
}

private:
Thread[] workers;
string uri;
string tag;

}


115 changes: 115 additions & 0 deletions nngd/nngtests/test06.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
module nngd.nngtests.test06;

import std.stdio;
import std.concurrency;
import std.exception;
import std.json;
import std.format;
import std.conv;
import std.regex;
import std.file;
import std.random;
import core.thread;
import core.thread.osthread;
import nngd;

const _testclass = "nngd.nngtests.nng_test06_message";

@trusted class nng_test06_message : NNGTest {

this(Args...)(auto ref Args args) { super(args); }

override string[] run(){
int rc;
string s = "AbAbAgAlAmAgA";
log("NNG test 06: nng message manupulation");

log("NNGMessage test 1: assembly-disassembly");
try{
NNGMessage msg1 = NNGMessage(0);

rc = msg1.body_append!ushort(11); enforce(rc == 0);
rc = msg1.body_append!uint(12); enforce(rc == 0);
rc = msg1.body_append!ulong(13); enforce(rc == 0);
rc = msg1.body_prepend(cast(ubyte[])s); enforce(rc == 0);

enforce( msg1.length == 27 && msg1.header_length == 0 );

auto x1 = msg1.body_chop!ulong();
enforce(x1 == 13);
auto x2 = msg1.body_chop!uint();
enforce(x2 == 12);
auto x3 = msg1.body_chop!ushort();
enforce(x3 == 11);
auto x4 = msg1.body_trim!(ubyte[])();
string x5 = cast(string)x4;
enforce(x5 == s);

enforce( msg1.length == 0 && msg1.header_length == 0 );
} catch(Throwable e) {
error(dump_exception_recursive(e, "test 1: assembly-disassembly"));
}
log("...passed");

log("NNGMessage test 2: send-receive");

string url = "tcp://127.0.0.1:13006";
try{
NNGSocket sr = NNGSocket(nng_socket_type.NNG_SOCKET_PULL);
sr.recvtimeout = msecs(1000);
rc = sr.listen(url);
enforce(rc == 0);
NNGSocket ss = NNGSocket(nng_socket_type.NNG_SOCKET_PUSH);
ss.sendtimeout = msecs(1000);
ss.sendbuf = 4096;
rc = ss.dial(url);
enforce(rc == 0);

NNGMessage msg2 = NNGMessage(0);
rc = msg2.body_append!ushort(11); enforce(rc == 0);
rc = msg2.body_append!uint(12); enforce(rc == 0);
rc = msg2.body_append!ulong(13); enforce(rc == 0);
rc = msg2.body_prepend(cast(ubyte[])s); enforce(rc == 0);
NNGMessage msg3 = NNGMessage(0);

rc = ss.sendmsg(msg2);
enforce(rc == 0);

rc = sr.receivemsg(&msg3);
enforce(rc == 0);

enforce( msg3.length == 27 && msg3.header_length == 0 );

auto x6 = msg3.body_trim!string(s.length);
enforce(x6 == s);
} catch(Throwable e) {
error(dump_exception_recursive(e, "test 2: send-receive"));
}

log("NNGMessage test 3: realloc");
try{
NNGMessage *msg4 = new NNGMessage(0);

ubyte[] data = cast(ubyte[])("/dev/urandom".read(8192));
auto rnd = Random(to!int(timestamp));
ulong k;
for(auto i=0; i<128; i++){
k = uniform(0,8192,rnd);
msg4.clear();
msg4.body_append(data[0..k]);
enforce(msg4.length == k);
}
} catch(Throwable e) {
error(dump_exception_recursive(e, "test 3: realloc"));
}

log("...passed");
log(_testclass ~ ": Bye!");
return [];
}


}



Loading

0 comments on commit d8c3de6

Please sign in to comment.