Implement some Connection category commands

This commit is contained in:
R. Tyler Croy 2019-01-06 16:51:17 -08:00
parent ba41d474dc
commit 5581fe0d3a
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
17 changed files with 231 additions and 57 deletions

View File

@ -212,13 +212,13 @@ Some error strings and codes may differ between Mozzoni and redis
| :x:
| `ECHO`
| :x:
| :frog:
| `PING`
| :frog:
| `QUIT`
| :x:
| :frog:
| `SELECT`
| :x:

View File

@ -0,0 +1,21 @@
#!/usr/bin/env python
import helpers
import random
import sys
import unittest
class ConnectionTest(helpers.MozzoniTest):
def test_ping(self):
self.r.ping()
def test_echo(self):
self.assertEqual('test',
self.r.echo('test'))
self.assertEqual('hello world',
self.r.echo('hello world'))
if __name__ == '__main__':
random.seed()
unittest.main()

View File

@ -1,10 +0,0 @@
#!/usr/bin/env python
import helpers
class PingTest(helpers.MozzoniTest):
def test_ping(self):
self.r.ping()
if __name__ == '__main__':
unittest.main()

View File

@ -47,7 +47,7 @@ package body Mozzoni.Client.Client_Type_Test_Data.Client_Type_Tests is
procedure Test_Is_Valid_ef2a98 (Gnattest_T : in out Test_Client_Type) renames Test_Is_Valid;
-- id:2.2/ef2a987f54e227e4/Is_Valid/1/0/
procedure Test_Is_Valid (Gnattest_T : in out Test_Client_Type) is
-- mozzoni-client.ads:29:4:Is_Valid
-- mozzoni-client.ads:31:4:Is_Valid
-- end read only
@ -67,7 +67,7 @@ package body Mozzoni.Client.Client_Type_Test_Data.Client_Type_Tests is
procedure Test_Read_Available_f8f154 (Gnattest_T : in out Test_Client_Type) renames Test_Read_Available;
-- id:2.2/f8f1540f1aba8e29/Read_Available/1/0/
procedure Test_Read_Available (Gnattest_T : in out Test_Client_Type) is
-- mozzoni-client.ads:34:4:Read_Available
-- mozzoni-client.ads:36:4:Read_Available
-- end read only
pragma Unreferenced (Gnattest_T);
@ -87,7 +87,7 @@ package body Mozzoni.Client.Client_Type_Test_Data.Client_Type_Tests is
procedure Test_Write_8c9d70 (Gnattest_T : in out Test_Client_Type) renames Test_1_Write;
-- id:2.2/8c9d7061edeeea0a/Write/1/0/
procedure Test_1_Write (Gnattest_T : in out Test_Client_Type) is
-- mozzoni-client.ads:40:4:Write
-- mozzoni-client.ads:42:4:Write
-- end read only
pragma Unreferenced (Gnattest_T);
@ -107,7 +107,7 @@ package body Mozzoni.Client.Client_Type_Test_Data.Client_Type_Tests is
procedure Test_Write_1a9cb5 (Gnattest_T : in out Test_Client_Type) renames Test_2_Write;
-- id:2.2/1a9cb59ddb076acf/Write/0/0/
procedure Test_2_Write (Gnattest_T : in out Test_Client_Type) is
-- mozzoni-client.ads:46:4:Write
-- mozzoni-client.ads:48:4:Write
-- end read only
pragma Unreferenced (Gnattest_T);
@ -127,7 +127,7 @@ package body Mozzoni.Client.Client_Type_Test_Data.Client_Type_Tests is
procedure Test_Write_7f6e4c (Gnattest_T : in out Test_Client_Type) renames Test_3_Write;
-- id:2.2/7f6e4cf27e1eb94b/Write/0/0/
procedure Test_3_Write (Gnattest_T : in out Test_Client_Type) is
-- mozzoni-client.ads:56:4:Write
-- mozzoni-client.ads:58:4:Write
-- end read only
pragma Unreferenced (Gnattest_T);
@ -147,7 +147,7 @@ package body Mozzoni.Client.Client_Type_Test_Data.Client_Type_Tests is
procedure Test_Write_Line_Ending_579bd3 (Gnattest_T : in out Test_Client_Type) renames Test_Write_Line_Ending;
-- id:2.2/579bd35a577c3831/Write_Line_Ending/1/0/
procedure Test_Write_Line_Ending (Gnattest_T : in out Test_Client_Type) is
-- mozzoni-client.ads:60:4:Write_Line_Ending
-- mozzoni-client.ads:62:4:Write_Line_Ending
-- end read only
pragma Unreferenced (Gnattest_T);
@ -167,7 +167,7 @@ package body Mozzoni.Client.Client_Type_Test_Data.Client_Type_Tests is
procedure Test_Client_For_bc4703 (Gnattest_T : in out Test_Client_Type) renames Test_1_Client_For;
-- id:2.2/bc4703174166b521/Client_For/1/0/
procedure Test_1_Client_For (Gnattest_T : in out Test_Client_Type) is
-- mozzoni-client.ads:89:4:Client_For
-- mozzoni-client.ads:91:4:Client_For
-- end read only
pragma Unreferenced (Gnattest_T);
@ -186,7 +186,7 @@ package body Mozzoni.Client.Client_Type_Test_Data.Client_Type_Tests is
procedure Test_Client_For_280f2c (Gnattest_T : in out Test_Client_Type) renames Test_2_Client_For;
-- id:2.2/280f2c7eee8955be/Client_For/0/0/
procedure Test_2_Client_For (Gnattest_T : in out Test_Client_Type) is
-- mozzoni-client.ads:96:4:Client_For
-- mozzoni-client.ads:98:4:Client_For
-- end read only
pragma Unreferenced (Gnattest_T);

View File

@ -36,7 +36,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Has_Client_08cb0c (Gnattest_T : in out Test) renames Test_Has_Client;
-- id:2.2/08cb0c23d0202b56/Has_Client/1/0/
procedure Test_Has_Client (Gnattest_T : in out Test) is
-- mozzoni-client.ads:66:4:Has_Client
-- mozzoni-client.ads:68:4:Has_Client
-- end read only
pragma Unreferenced (Gnattest_T);
@ -57,7 +57,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Register_Client_54a4ba (Gnattest_T : in out Test) renames Test_Register_Client;
-- id:2.2/54a4ba3d8480e0fd/Register_Client/1/0/
procedure Test_Register_Client (Gnattest_T : in out Test) is
-- mozzoni-client.ads:71:4:Register_Client
-- mozzoni-client.ads:73:4:Register_Client
-- end read only
pragma Unreferenced (Gnattest_T);
@ -77,7 +77,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Deregister_Client_cc045f (Gnattest_T : in out Test) renames Test_Deregister_Client;
-- id:2.2/cc045f6ce0713995/Deregister_Client/1/0/
procedure Test_Deregister_Client (Gnattest_T : in out Test) is
-- mozzoni-client.ads:77:4:Deregister_Client
-- mozzoni-client.ads:79:4:Deregister_Client
-- end read only
pragma Unreferenced (Gnattest_T);
@ -101,7 +101,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Dump_Status_ee75bc (Gnattest_T : in out Test) renames Test_Dump_Status;
-- id:2.2/ee75bcb86b376fe3/Dump_Status/1/0/
procedure Test_Dump_Status (Gnattest_T : in out Test) is
-- mozzoni-client.ads:83:4:Dump_Status
-- mozzoni-client.ads:85:4:Dump_Status
-- end read only
pragma Unreferenced (Gnattest_T);
@ -120,7 +120,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Hash_Descriptor_c833a5 (Gnattest_T : in out Test) renames Test_Hash_Descriptor;
-- id:2.2/c833a53e886af8a2/Hash_Descriptor/1/0/
procedure Test_Hash_Descriptor (Gnattest_T : in out Test) is
-- mozzoni-client.ads:103:4:Hash_Descriptor
-- mozzoni-client.ads:105:4:Hash_Descriptor
-- end read only
pragma Unreferenced (Gnattest_T);
@ -142,7 +142,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Read_Socket_609c7d (Gnattest_T : in out Test) renames Test_Read_Socket;
-- id:2.2/609c7ddfc531d340/Read_Socket/1/0/
procedure Test_Read_Socket (Gnattest_T : in out Test) is
-- mozzoni-client.ads:114:4:Read_Socket
-- mozzoni-client.ads:116:4:Read_Socket
-- end read only
pragma Unreferenced (Gnattest_T);
@ -162,7 +162,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Write_Socket_f63b4b (Gnattest_T : in out Test) renames Test_Write_Socket;
-- id:2.2/f63b4b647f6092f6/Write_Socket/1/0/
procedure Test_Write_Socket (Gnattest_T : in out Test) is
-- mozzoni-client.ads:121:4:Write_Socket
-- mozzoni-client.ads:123:4:Write_Socket
-- end read only
pragma Unreferenced (Gnattest_T);
@ -182,7 +182,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Close_Socket_73424d (Gnattest_T : in out Test) renames Test_Close_Socket;
-- id:2.2/73424da53916b082/Close_Socket/1/0/
procedure Test_Close_Socket (Gnattest_T : in out Test) is
-- mozzoni-client.ads:128:4:Close_Socket
-- mozzoni-client.ads:130:4:Close_Socket
-- end read only
pragma Unreferenced (Gnattest_T);
@ -202,7 +202,7 @@ package body Mozzoni.Client.Test_Data.Tests is
procedure Test_Flush_All_Clients_a897d3 (Gnattest_T : in out Test) renames Test_Flush_All_Clients;
-- id:2.2/a897d32357bb297b/Flush_All_Clients/1/0/
procedure Test_Flush_All_Clients (Gnattest_T : in out Test) is
-- mozzoni-client.ads:135:4:Flush_All_Clients
-- mozzoni-client.ads:137:4:Flush_All_Clients
-- end read only
pragma Unreferenced (Gnattest_T);

View File

@ -0,0 +1,98 @@
-- This package has been generated automatically by GNATtest.
-- You are allowed to add your code to the bodies of test routines.
-- Such changes will be kept during further regeneration of this file.
-- All code placed outside of test routine bodies will be lost. The
-- code intended to set up and tear down the test environment should be
-- placed into Mozzoni.Commands.Connection.Test_Data.
with AUnit.Assertions; use AUnit.Assertions;
with System.Assertions;
-- begin read only
-- id:2.2/00/
--
-- This section can be used to add with clauses if necessary.
--
-- end read only
-- begin read only
-- end read only
package body Mozzoni.Commands.Connection.Test_Data.Tests is
-- begin read only
-- id:2.2/01/
--
-- This section can be used to add global variables and other elements.
--
-- end read only
-- begin read only
-- end read only
-- begin read only
procedure Test_Handle_Ping (Gnattest_T : in out Test);
procedure Test_Handle_Ping_9b89ba (Gnattest_T : in out Test) renames Test_Handle_Ping;
-- id:2.2/9b89baf367524907/Handle_Ping/1/0/
procedure Test_Handle_Ping (Gnattest_T : in out Test) is
-- mozzoni-commands-connection.ads:3:4:Handle_Ping
-- end read only
pragma Unreferenced (Gnattest_T);
begin
null;
-- begin read only
end Test_Handle_Ping;
-- end read only
-- begin read only
procedure Test_Handle_Echo (Gnattest_T : in out Test);
procedure Test_Handle_Echo_b911eb (Gnattest_T : in out Test) renames Test_Handle_Echo;
-- id:2.2/b911eb50a32ed8da/Handle_Echo/1/0/
procedure Test_Handle_Echo (Gnattest_T : in out Test) is
-- mozzoni-commands-connection.ads:7:4:Handle_Echo
-- end read only
pragma Unreferenced (Gnattest_T);
begin
null;
-- begin read only
end Test_Handle_Echo;
-- end read only
-- begin read only
procedure Test_Handle_Quit (Gnattest_T : in out Test);
procedure Test_Handle_Quit_d26148 (Gnattest_T : in out Test) renames Test_Handle_Quit;
-- id:2.2/d2614848b39cc75f/Handle_Quit/1/0/
procedure Test_Handle_Quit (Gnattest_T : in out Test) is
-- mozzoni-commands-connection.ads:11:4:Handle_Quit
-- end read only
pragma Unreferenced (Gnattest_T);
begin
null;
-- begin read only
end Test_Handle_Quit;
-- end read only
-- begin read only
-- id:2.2/02/
--
-- This section can be used to add elaboration code for the global state.
--
begin
-- end read only
null;
-- begin read only
-- end read only
end Mozzoni.Commands.Connection.Test_Data.Tests;

View File

@ -0,0 +1,20 @@
-- This package is intended to set up and tear down the test environment.
-- Once created by GNATtest, this package will never be overwritten
-- automatically. Contents of this package can be modified in any way
-- except for sections surrounded by a 'read only' marker.
package body Mozzoni.Commands.Connection.Test_Data is
procedure Set_Up (Gnattest_T : in out Test) is
pragma Unreferenced (Gnattest_T);
begin
null;
end Set_Up;
procedure Tear_Down (Gnattest_T : in out Test) is
pragma Unreferenced (Gnattest_T);
begin
null;
end Tear_Down;
end Mozzoni.Commands.Connection.Test_Data;

View File

@ -78,23 +78,22 @@ package body Mozzoni.Commands.Test_Data.Tests is
-- begin read only
procedure Test_Handle_Ping (Gnattest_T : in out Test);
procedure Test_Handle_Ping_9b89ba (Gnattest_T : in out Test) renames Test_Handle_Ping;
-- id:2.2/9b89baf367524907/Handle_Ping/1/0/
procedure Test_Handle_Ping (Gnattest_T : in out Test) is
-- mozzoni-commands.ads:14:4:Handle_Ping
-- procedure Test_Handle_Ping (Gnattest_T : in out Test_);
-- procedure Test_Handle_Ping_9b89ba (Gnattest_T : in out Test_) renames Test_Handle_Ping;
-- id:2.2/9b89baf367524907/Handle_Ping/1/1/
-- procedure Test_Handle_Ping (Gnattest_T : in out Test_) is
-- end read only
pragma Unreferenced (Gnattest_T);
begin
-- No-opping this test, not really worth the effort. Acceptance tests cover
-- this.
null;
--
-- pragma Unreferenced (Gnattest_T);
--
-- begin
--
-- -- No-opping this test, not really worth the effort. Acceptance tests cover
-- -- this.
-- null;
--
-- begin read only
end Test_Handle_Ping;
-- end Test_Handle_Ping;
-- end read only
-- begin read only

View File

@ -162,7 +162,6 @@ begin
exception
when Err : others =>
Log.Error (Ada.Exceptions.Exception_Message (Err));
raise;
end;
elsif Disconnecting then

View File

@ -1,4 +1,5 @@
with Ada.Characters.Latin_1;
with Ada.Exceptions;
with Mozzoni.Dispatch; use Mozzoni.Dispatch;
@ -258,6 +259,11 @@ package body Mozzoni.Client is
else
Log.Warn ("Not yet enough data to dispatch!");
end if;
exception
when Disconnect_Client =>
Close_Socket (Client.Socket);
when Err : others =>
Log.Error (Ada.Exceptions.Exception_Message (Err));
end Parse_Available;

View File

@ -9,6 +9,8 @@ private with Ada.Characters.Latin_1;
package Mozzoni.Client is
Disconnect_Client : exception;
type Client_Type is tagged record
Descriptor : Integer;
Socket : Socket_Type;

View File

@ -1,5 +1,6 @@
with Mozzoni.Commands;
with Mozzoni.Commands.Connection;
with Mozzoni.Commands.Keys;
with Mozzoni.Dispatch; use Mozzoni.Dispatch;
@ -7,7 +8,9 @@ package body Mozzoni.Command_Loader is
procedure Load is
begin
Register_Command ("PING", Mozzoni.Commands.Handle_Ping'Access);
Register_Command ("PING", Mozzoni.Commands.Connection.Handle_Ping'Access);
Register_Command ("ECHO", Mozzoni.Commands.Connection.Handle_Echo'Access);
Register_Command ("QUIT", Mozzoni.Commands.Connection.Handle_Quit'Access);
-- Register the key related commands (e.g. SET, GET, etc)
Register_Command ("SET", Mozzoni.Commands.Keys.Handle_Set'Access);

View File

@ -0,0 +1,30 @@
package body Mozzoni.Commands.Connection is
procedure Handle_Ping (Client : in out Client_Type;
Command : Command_Array_Access) is
begin
Client.Write (Prepare_Response ("+PONG"));
end Handle_Ping;
procedure Handle_Echo (Client : in out Client_Type;
Command : Command_Array_Access) is
Payload : constant Command_Item := Command (2);
begin
Client.Write ('$');
Client.Write (Length (Payload.Value));
Client.Write_Line_Ending;
Client.Write (To_String (Payload.Value));
Client.Write_Line_Ending;
end Handle_Echo;
procedure Handle_Quit (Client : in out Client_Type;
Command : Command_Array_Access) is
begin
Client.Write (Prepare_Response ("+OK"));
raise Disconnect_Client with "Client requested QUIT";
end Handle_Quit;
end Mozzoni.Commands.Connection;

View File

@ -0,0 +1,16 @@
package Mozzoni.Commands.Connection is
procedure Handle_Ping (Client : in out Client_Type;
Command : Command_Array_Access)
with Pre => Client.Is_Valid and Command /= null;
procedure Handle_Echo (Client : in out Client_Type;
Command : Command_Array_Access)
with Pre => Client.Is_Valid and Command /= null;
procedure Handle_Quit (Client : in out Client_Type;
Command : Command_Array_Access)
with Pre => Client.Is_Valid and Command /= null;
end Mozzoni.Commands.Connection;

View File

@ -111,7 +111,7 @@ package body Mozzoni.Commands.Keys is
Value : constant Value_Type := KeyValue.Get (Key);
begin
Client.Write (Length (Value.Buffer));
Client.Write (CR & LF);
Client.Write_Line_Ending;
Client.Write (Prepare_Response (Value.Buffer));
end;

View File

@ -15,10 +15,4 @@ package body Mozzoni.Commands is
end Prepare_Response;
procedure Handle_Ping (Client : in out Client_Type;
Command : Command_Array_Access) is
begin
Client.Write (Prepare_Response ("+PONG"));
end Handle_Ping;
end Mozzoni.Commands;

View File

@ -11,8 +11,4 @@ package Mozzoni.Commands is
function Prepare_Response (Value : in Unbounded_String) return String
with Pre => Length (Value) > 0;
procedure Handle_Ping (Client : in out Client_Type;
Command : Command_Array_Access)
with Pre => Client.Is_Valid and Command /= null;
end Mozzoni.Commands;