delphimvcframework/unittests/DMVCFrameworkTests/MessagingExtensionsTestU.pas

195 lines
5.9 KiB
ObjectPascal
Raw Normal View History

2013-10-30 01:09:09 +01:00
unit MessagingExtensionsTestU;
interface
uses
TestFramework,
MVCFramework.RESTClient,
LiveServerTestU;
type
TMessagingExtensionsTestCase = class(TBaseServerTest)
published
procedure TestSubscribeOnATopic;
procedure TestMultipleSubscribeOnSameTopic;
procedure TestMultipleSubscribeAndUnsubscribe;
procedure TestMultipleSubscribeAndUnsubscribeHARD;
procedure TestSubscribeAndReceive;
end;
implementation
uses
System.SysUtils,
Data.DBXJSON,
System.Classes,
MVCFramework.Logger;
{ TMessagingExtensionsTestCase }
procedure TMessagingExtensionsTestCase.TestMultipleSubscribeAndUnsubscribe;
var
res: IRESTResponse;
2013-11-09 10:05:14 +01:00
x: string;
2013-10-30 01:09:09 +01:00
begin
2013-11-09 10:05:14 +01:00
RESTClient.ReadTimeout := -1;
2013-10-30 01:09:09 +01:00
DoLoginWith('d.teti');
res := RESTClient.doGET('/messages/subscribe', ['test01']);
CheckEquals(200, res.ResponseCode, res.ResponseText);
res := RESTClient.doGET('/messages/topics', []);
x := Trim(res.BodyAsString);
CheckEquals('/topic/test01', x);
res := RESTClient.doGET('/messages/subscribe', ['test010']);
2013-11-09 10:05:14 +01:00
CheckEquals(200, res.ResponseCode, res.ResponseText);
// server shod not return an error
2013-10-30 01:09:09 +01:00
res := RESTClient.doGET('/messages/topics', []);
x := Trim(res.BodyAsString);
CheckEquals('/topic/test01;/topic/test010', x);
res := RESTClient.doGET('/messages/unsubscribe', ['test01']);
2013-11-09 10:05:14 +01:00
CheckEquals(200, res.ResponseCode, res.ResponseText);
// server shod not return an error
2013-10-30 01:09:09 +01:00
res := RESTClient.doGET('/messages/topics', []);
x := Trim(res.BodyAsString);
CheckEquals('/topic/test010', x);
2013-11-09 10:05:14 +01:00
CheckEquals(200, res.ResponseCode, res.ResponseText);
// server shod not return an error
2013-10-30 01:09:09 +01:00
DoLogout;
end;
procedure TMessagingExtensionsTestCase.TestMultipleSubscribeAndUnsubscribeHARD;
var
res: IRESTResponse;
2013-11-09 10:05:14 +01:00
x: string;
2013-10-30 01:09:09 +01:00
begin
2013-11-09 10:05:14 +01:00
RESTClient.ReadTimeout := -1;
2013-10-30 01:09:09 +01:00
DoLoginWith('d.teti');
res := RESTClient.doGET('/messages/subscribe', ['test01']);
res := RESTClient.doGET('/messages/subscribe', ['test010']);
res := RESTClient.doGET('/messages/subscribe', ['test0101']);
res := RESTClient.doGET('/messages/subscribe', ['test01010']);
res := RESTClient.doGET('/messages/subscribe', ['test010101']);
res := RESTClient.doGET('/messages/subscribe', ['test0101010']);
2013-11-09 10:05:14 +01:00
CheckEquals(200, res.ResponseCode, res.ResponseText);
// server shod not return an error
2013-10-30 01:09:09 +01:00
res := RESTClient.doGET('/messages/topics', []);
x := Trim(res.BodyAsString);
CheckEquals
('/topic/test01;/topic/test010;/topic/test0101;/topic/test01010;/topic/test010101;/topic/test0101010',
x);
res := RESTClient.doGET('/messages/unsubscribe', ['test010']);
2013-11-09 10:05:14 +01:00
CheckEquals(200, res.ResponseCode, res.ResponseText);
// server shod not return an error
2013-10-30 01:09:09 +01:00
res := RESTClient.doGET('/messages/topics', []);
x := Trim(res.BodyAsString);
CheckEquals
('/topic/test01;/topic/test0101;/topic/test01010;/topic/test010101;/topic/test0101010',
x);
DoLogout;
end;
procedure TMessagingExtensionsTestCase.TestMultipleSubscribeOnSameTopic;
var
res: IRESTResponse;
begin
DoLoginWith('d.teti');
res := RESTClient.doGET('/messages/subscribe', ['test01']);
CheckEquals(200, res.ResponseCode, res.ResponseText);
res := RESTClient.doGET('/messages/subscribe', ['test01']);
2013-11-09 10:05:14 +01:00
CheckEquals(200, res.ResponseCode, res.ResponseText);
// server shod not return an error
2013-10-30 01:09:09 +01:00
DoLogout;
end;
procedure TMessagingExtensionsTestCase.TestSubscribeAndReceive;
var
2013-11-09 10:05:14 +01:00
res: IRESTResponse;
messages: TJSONArray;
sid: string;
2013-10-30 01:09:09 +01:00
RMessageCount: Integer;
2013-11-09 10:05:14 +01:00
I: Integer;
o: TJSONObject;
2013-10-30 01:09:09 +01:00
const
MSG_COUNT = 100;
begin
DoLoginWith('d.teti');
RESTClient.doGET('/messages/subscribe/test01', []);
RESTClient.doGET('/messages/subscribe/test02', []);
2013-11-09 10:05:14 +01:00
RESTClient.ReadTimeout := -1;
2013-10-30 01:09:09 +01:00
RESTClient.doPOST('/messages/enqueue/test02', [],
TJSONObject.Create(TJSONPair.Create('hello', 'world')));
sid := RESTClient.SessionID;
TThread.CreateAnonymousThread(
procedure
var
RESTC: TRESTClient;
I: Integer;
begin
TThread.Sleep(1000);
RESTC := TRESTClient.Create('localhost', 8888);
try
RESTC.ReadTimeout := 60 * 1000 * 30;
RESTC.doGET('/login', ['j.doe']);
for I := 1 to MSG_COUNT do
begin
RESTC.doPOST('/messages/enqueue/test02', [],
2013-11-09 10:05:14 +01:00
TJSONObject.Create(TJSONPair.Create('hello',
TJSONNumber.Create(I))));
2013-10-30 01:09:09 +01:00
end;
finally
RESTC.Free;
end;
end).Start;
RMessageCount := 0;
while RMessageCount < MSG_COUNT do
begin
res := RESTClient.doGET('/messages/receive', []);
if res.ResponseCode = 200 then
begin
2013-11-09 10:05:14 +01:00
CheckNotNull(res.BodyAsJsonObject.Get('_timestamp'),
'_timestamp is not set');
2013-10-30 01:09:09 +01:00
CheckNotNull(res.BodyAsJsonObject.Get('messages'), 'messages is not set');
CheckIs(res.BodyAsJsonObject.Get('messages').JsonValue, TJSONArray,
'Messages is not a TJSONArray');
messages := res.BodyAsJsonObject.Get('messages').JsonValue as TJSONArray;
if messages.Size > 0 then
for I := 0 to messages.Size - 1 do
begin
o := messages.Get(I) as TJSONObject;
logw(o.Get('message').toString);
end;
RMessageCount := RMessageCount + messages.Size;
end;
if res.ResponseCode = 204 then // receive timeout
break;
end;
CheckEquals(MSG_COUNT, RMessageCount, 'message count');
DoLogout;
end;
procedure TMessagingExtensionsTestCase.TestSubscribeOnATopic;
var
res: IRESTResponse;
begin
DoLoginWith('d.teti');
res := RESTClient.doGET('/messages/subscribe', ['test01']);
CheckEquals(200, res.ResponseCode, res.ResponseText);
res := RESTClient.doGET('/messages/unsubscribe', ['test01']);
CheckEquals(200, res.ResponseCode, res.ResponseText);
DoLogout;
end;
initialization
2013-11-09 10:05:14 +01:00
// RegisterTest(TMessagingExtensionsTestCase.Suite);
finalization
2013-10-30 01:09:09 +01:00
end.