Skip to content

Commit 62986c2

Browse files
committed
tests validator
1 parent 62d9d8c commit 62986c2

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

misc/test_validator.cpp

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#include <fstream>
2+
#include "../include/Binance_Client.h"
3+
4+
#include <thread>
5+
#include <chrono>
6+
#include <iostream>
7+
8+
9+
bool auth_mode = 1;
10+
std::string __KEYS_PATH = "keys_conf.json";
11+
12+
struct SomeFunctor
13+
{
14+
Json::CharReaderBuilder charbuilder;
15+
Json::CharReader* charreader;
16+
std::string parse_errors;
17+
std::string msg_buffer;
18+
Json::Value stream_msg;
19+
20+
SomeFunctor()
21+
: msg_buffer{ "" }, parse_errors{ }, charreader{ charbuilder.newCharReader() }
22+
{}
23+
24+
25+
SomeFunctor operator()(const std::string& response)
26+
{
27+
this->stream_msg.clear();
28+
this->parse_errors.clear();
29+
30+
this->charreader->parse(response.c_str(),
31+
response.c_str() + response.size(),
32+
&this->stream_msg,
33+
&parse_errors);
34+
35+
std::cout << this->stream_msg;
36+
37+
return *this;
38+
}
39+
};
40+
41+
Json::Value load_keys(std::string conf_filename) // return return type
42+
{
43+
Json::Value keys_json;
44+
std::ifstream file_handle(__KEYS_PATH);
45+
if (!file_handle.is_open())
46+
{
47+
std::cout << "failed to load keys json" << std::endl;
48+
exit(1);
49+
};
50+
51+
file_handle >> keys_json;
52+
53+
return keys_json;
54+
}
55+
56+
int main()
57+
{
58+
try
59+
{
60+
// ========================================================== Client Tests
61+
62+
std::cout << "\n\n\nStarting Client tests...\n\n";
63+
64+
Json::Value keys;
65+
66+
if (auth_mode) keys = load_keys(__KEYS_PATH);
67+
68+
SpotClient my_client{ keys["key"].asString(), keys["secret"].asString() }; // private
69+
SpotClient::MarginAccount margin_acc{ my_client };
70+
71+
FuturesClientUSDT my_f_client{}; // public
72+
73+
try
74+
{
75+
std::cout << "\nattemping private struct on public client\n";
76+
FuturesClientUSDT::FuturesWallet my_f_wallet{ my_f_client };
77+
}
78+
catch (ClientException& e)
79+
{
80+
std::cout << "\nresult of attempt: " << e.what();
81+
}
82+
83+
84+
// ========================================================== WS Tests
85+
86+
std::cout << "\n\n\nStarting WS tests...\n\n";
87+
88+
std::string str_buf{};
89+
SomeFunctor ws_stream_read{};
90+
91+
std::string cust_stream = "btcusdt@aggTrade/ethusdt@aggTrade";
92+
my_client.set_refresh_key_interval(1);
93+
std::cout << "\nstarting custom stream + ping\n";
94+
std::thread t1(&SpotClient::custom_stream<SomeFunctor>, std::ref(my_client), cust_stream, std::ref(str_buf), std::ref(ws_stream_read), 1);
95+
Sleep(2000);
96+
std::cout << "\nis stream open? " << my_client.is_stream_open(cust_stream);
97+
std::cout << "\ngoing to sleep for 3 seconds";
98+
Sleep(3000);
99+
std::cout << "\nterminating custom stream";
100+
my_client.close_stream(cust_stream);
101+
std::cout << "\ncustom stream was terminated";
102+
103+
std::cout << "\nstarting aggTrade stream\n";
104+
std::thread t2(&SpotClient::stream_aggTrade<SomeFunctor>, std::ref(my_client), "btcusdt", std::ref(str_buf), std::ref(ws_stream_read));
105+
std::cout << "\ngoing to sleep for 2 seconds";
106+
Sleep(2000);
107+
std::cout << "\nterminating aggTrade stream";
108+
my_client.close_stream("btcusdt@aggTrade");
109+
std::cout << "\naggTrade stream was terminated";
110+
111+
t1.join();
112+
t2.join();
113+
Sleep(2000);
114+
115+
// ========================================================== REST Tests
116+
117+
std::cout << "\n\n\nStarting REST tests...\n\n";
118+
119+
Params temp_params{};
120+
temp_params.set_param<std::string>("symbol", "BTCUSDT");
121+
Params order_params{ Params{} };
122+
order_params = Params{};
123+
124+
std::cout << "\nfetching all orders spotclient:\n";
125+
std::cout << my_client.all_orders(&temp_params) << "\n";
126+
127+
std::cout << "\nfetching all orders futuresclient:\n";
128+
std::cout << my_f_client.all_orders(&temp_params) << "\n";
129+
130+
std::cout << "\nMarginAccount default not isolated listen_key: " << margin_acc.margin_get_listen_key() << "\n";
131+
std::cout << "\nMarginAccount isolated listen_key: " << margin_acc.margin_isolated_get_listen_key("btcusdt") << "\n";
132+
133+
134+
std::cout << "\testing custom request with params: \n" << my_client.custom_get_req("https://api.binance.com", "/api/v3/time", &order_params, 0);
135+
136+
137+
// ========================================================== Exceptions
138+
139+
std::cout << "\n\n\nStarting Exceptions tests...\n\n";
140+
141+
try
142+
{
143+
BadRequestREST e{};
144+
throw(e);
145+
}
146+
catch (std::exception& e)
147+
{
148+
std::cout << e.what() << "\n";
149+
std::cout << "\nBadRequestREST was caught\n";
150+
}
151+
152+
try
153+
{
154+
BadSetupHeadersREST e{};
155+
throw(e);
156+
}
157+
catch (ClientException& e)
158+
{
159+
std::cout << e.what() << "\n";
160+
std::cout << "\nBadSetupHeadersREST was caught\n";
161+
}
162+
163+
try
164+
{
165+
CustomException e("custom exception");
166+
throw(e);
167+
}
168+
catch (ClientException& e)
169+
{
170+
std::cout << e.what() << "\n";
171+
std::cout << "\nCustomException was caught\n";
172+
}
173+
174+
}
175+
catch (ClientException& e)
176+
{
177+
std::cout << "\nan exception has occurred: " << e.what();
178+
}
179+
std::cout << "finished successfully";
180+
return 0;
181+
}

0 commit comments

Comments
 (0)