Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xs-test
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patrick Chen
xs-test
Commits
0b058194
Commit
0b058194
authored
Aug 27, 2018
by
Patrick Chen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
handle command/event and give out response
parent
9331faaa
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
116 additions
and
12 deletions
+116
-12
Config.h
include/test/Config.h
+9
-1
Service.h
include/test/Service.h
+3
-0
Tester.h
include/test/Tester.h
+9
-0
config.json
res/example/config.json
+12
-0
Config.cc
src/test/Config.cc
+10
-0
Service.cc
src/test/Service.cc
+10
-0
Tester.cc
src/test/Tester.cc
+61
-1
xs-test.cc
src/xs-test.cc
+2
-10
No files found.
include/test/Config.h
View file @
0b058194
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
#include <memory>
#include <memory>
#include <boost/filesystem.hpp>
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <nlohmann/json.hpp>
#include <nlohmann/json.hpp>
#include "test/Service.h"
#include "test/Service.h"
...
@@ -14,6 +15,9 @@ namespace xs { namespace test
...
@@ -14,6 +15,9 @@ namespace xs { namespace test
{
{
class
Config
final
class
Config
final
{
{
private
:
using
service_range_type
=
std
::
deque
<
Service
>
;
public
:
public
:
Config
()
=
delete
;
Config
()
=
delete
;
explicit
Config
(
const
boost
::
filesystem
::
path
&
file
);
explicit
Config
(
const
boost
::
filesystem
::
path
&
file
);
...
@@ -24,10 +28,14 @@ namespace xs { namespace test
...
@@ -24,10 +28,14 @@ namespace xs { namespace test
Config
&
operator
=
(
const
Config
&
)
=
delete
;
Config
&
operator
=
(
const
Config
&
)
=
delete
;
Config
&
operator
=
(
Config
&&
)
=
delete
;
Config
&
operator
=
(
Config
&&
)
=
delete
;
boost
::
iterator_range
<
service_range_type
::
const_iterator
>
getServices
()
const
noexcept
;
private
:
private
:
static
bool
verify
(
const
nlohmann
::
json
&
object
)
noexcept
;
static
bool
verify
(
const
nlohmann
::
json
&
object
)
noexcept
;
p
ublic
:
p
rivate
:
std
::
deque
<
Service
>
services_
;
std
::
deque
<
Service
>
services_
;
};
};
}
}
}
}
...
...
include/test/Service.h
View file @
0b058194
...
@@ -28,6 +28,9 @@ namespace xs { namespace test
...
@@ -28,6 +28,9 @@ namespace xs { namespace test
explicit
Service
(
const
nlohmann
::
json
&
object
);
explicit
Service
(
const
nlohmann
::
json
&
object
);
static
bool
verify
(
const
nlohmann
::
json
&
object
)
noexcept
;
static
bool
verify
(
const
nlohmann
::
json
&
object
)
noexcept
;
bool
isCommand
()
const
noexcept
;
bool
isEvent
()
const
noexcept
;
private
:
private
:
Service
(
Type
type
,
std
::
string
name
,
nlohmann
::
json
response
)
noexcept
;
Service
(
Type
type
,
std
::
string
name
,
nlohmann
::
json
response
)
noexcept
;
...
...
include/test/Tester.h
View file @
0b058194
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
#define XS_TEST_TESTER_H_INCLUDED
#define XS_TEST_TESTER_H_INCLUDED
#include <memory>
#include <memory>
#include <unordered_map>
#include <boost/filesystem.hpp>
#include <boost/filesystem.hpp>
...
@@ -25,11 +26,19 @@ namespace xs { namespace test
...
@@ -25,11 +26,19 @@ namespace xs { namespace test
explicit
Tester
(
const
boost
::
filesystem
::
path
&
file
);
explicit
Tester
(
const
boost
::
filesystem
::
path
&
file
);
void
wait
()
noexcept
;
private
:
private
:
std
::
string
onCommand
(
const
std
::
string
&
name
,
const
std
::
string
&
params
)
noexcept
;
void
onEvent
(
const
std
::
string
&
name
,
const
std
::
string
&
params
)
noexcept
;
void
registerServices
()
noexcept
;
public
:
public
:
Config
config_
;
Config
config_
;
ExchangeServer
server_
;
ExchangeServer
server_
;
std
::
unordered_map
<
std
::
string
,
const
Service
*>
acceptCommands_
;
std
::
unordered_map
<
std
::
string
,
const
Service
*>
acceptEvents_
;
};
};
}
}
}
}
...
...
res/example/config.json
0 → 100644
View file @
0b058194
{
"mockData"
:
[
{
"command"
:
"aaa"
,
"response"
:
"123"
},
{
"event"
:
"bbb"
,
"response"
:
{
"value"
:
456
}
}
]
}
src/test/Config.cc
View file @
0b058194
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
#include <nlohmann/json.hpp>
#include <nlohmann/json.hpp>
#include <fstream>
#include <fstream>
#include <iterator>
#include <memory>
#include <memory>
#include <sstream>
#include <sstream>
#include <stdexcept>
#include <stdexcept>
...
@@ -42,6 +43,15 @@ Config::Config(const boost::filesystem::path& file)
...
@@ -42,6 +43,15 @@ Config::Config(const boost::filesystem::path& file)
}
}
}
}
boost
::
iterator_range
<
Config
::
service_range_type
::
const_iterator
>
Config
::
getServices
()
const
noexcept
{
return
boost
::
make_iterator_range
(
std
::
begin
(
services_
),
std
::
end
(
services_
)
);
}
bool
Config
::
verify
(
const
nlohmann
::
json
&
object
)
noexcept
bool
Config
::
verify
(
const
nlohmann
::
json
&
object
)
noexcept
{
{
if
(
!
(
object
.
count
(
"mockData"
)
&&
object
[
"mockData"
].
is_array
()))
{
if
(
!
(
object
.
count
(
"mockData"
)
&&
object
[
"mockData"
].
is_array
()))
{
...
...
src/test/Service.cc
View file @
0b058194
...
@@ -43,4 +43,14 @@ namespace xs { namespace test
...
@@ -43,4 +43,14 @@ namespace xs { namespace test
,
name_
(
std
::
move
(
name
))
,
name_
(
std
::
move
(
name
))
,
response_
(
std
::
move
(
response
))
,
response_
(
std
::
move
(
response
))
{
}
{
}
bool
Service
::
isCommand
()
const
noexcept
{
return
type_
==
Type
::
COMMAND
;
}
bool
Service
::
isEvent
()
const
noexcept
{
return
type_
==
Type
::
EVENT
;
}
}
}
}
}
src/test/Tester.cc
View file @
0b058194
#include "test/Service.h"
#include "test/Tester.h"
#include "test/Tester.h"
#include <boost/range/adaptors.hpp>
#include <chrono>
#include <functional>
#include <thread>
namespace
xs
{
namespace
test
{
namespace
xs
{
namespace
test
{
...
@@ -9,6 +15,60 @@ Tester::Tester(const boost::filesystem::path& file)
...
@@ -9,6 +15,60 @@ Tester::Tester(const boost::filesystem::path& file)
,
server_
(
,
server_
(
"xs-test"
,
{
},
{
},
0
,
nullptr
"xs-test"
,
{
},
{
},
0
,
nullptr
)
)
{
}
{
registerServices
();
}
void
Tester
::
registerServices
()
noexcept
{
acceptCommands_
.
clear
();
acceptEvents_
.
clear
();
using
namespace
std
::
placeholders
;
for
(
const
auto
&
service
:
config_
.
getServices
())
{
if
(
service
.
isCommand
())
{
server_
.
onCmd
(
service
.
name_
,
std
::
bind
(
&
Tester
::
onCommand
,
this
,
_1
,
_2
)
);
acceptCommands_
[
service
.
name_
]
=
&
service
;
}
else
{
server_
.
onEvent
(
service
.
name_
,
std
::
bind
(
&
Tester
::
onEvent
,
this
,
_1
,
_2
)
);
acceptEvents_
[
service
.
name_
]
=
&
service
;
}
}
}
std
::
string
Tester
::
onCommand
(
const
std
::
string
&
name
,
const
std
::
string
&
params
)
noexcept
{
const
auto
found
=
acceptCommands_
.
find
(
name
);
const
auto
*
service
=
std
::
get
<
1
>
(
*
found
);
const
auto
&
response
=
service
->
response_
;
/// TODO: keep track the status
return
response
.
is_string
()
?
response
.
get
<
std
::
string
>
()
:
response
.
dump
();
}
void
Tester
::
onEvent
(
const
std
::
string
&
name
,
const
std
::
string
&
params
)
noexcept
{
const
auto
found
=
acceptEvents_
.
find
(
name
);
const
auto
*
service
=
std
::
get
<
1
>
(
*
found
);
const
auto
&
response
=
service
->
response_
;
/// TODO: keep track the status
}
void
Tester
::
wait
()
noexcept
{
using
namespace
std
::
chrono_literals
;
while
(
true
)
{
std
::
this_thread
::
sleep_for
(
1
s
);
}
}
}
}
}
}
src/xs-test.cc
View file @
0b058194
...
@@ -21,14 +21,6 @@ int main(int argc, char* argv[]) {
...
@@ -21,14 +21,6 @@ int main(int argc, char* argv[]) {
return
EXIT_FAILURE
;
return
EXIT_FAILURE
;
}
}
using
namespace
xs
::
test
;
xs
::
test
::
Tester
tester
{
configFile
};
const
Tester
tester
{
configFile
};
tester
.
wait
();
for
(
const
auto
&
service
:
tester
.
config_
.
services_
)
{
if
(
service
.
type_
==
Service
::
Type
::
COMMAND
)
{
std
::
cout
<<
"command: "
<<
service
.
name_
;
}
else
{
std
::
cout
<<
"event: "
<<
service
.
name_
;
}
std
::
cout
<<
", response: "
<<
service
.
response_
<<
std
::
endl
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment