Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2578004
kazvjobtest.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
kazvjobtest.cpp
View Options
/*
* Copyright (C) 2020-2021 Tusooa Zhu <tusooa@kazv.moe>
*
* This file is part of libkazv.
*
* libkazv is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* libkazv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with libkazv. If not, see <https://www.gnu.org/licenses/>.
*/
#include
<libkazv-config.hpp>
#include
<vector>
#include
<iostream>
#include
<filesystem>
#include
<catch2/catch.hpp>
#include
<cprjobhandler.hpp>
#include
"tests.hpp"
#include
"kazvtest-respath.hpp"
#include
"file-guard.hpp"
using
namespace
Kazv
;
TEST_CASE
(
"setTimeout should behave properly"
,
"[kazvjob]"
)
{
boost
::
asio
::
io_context
ioContext
;
CprJobHandler
h
(
ioContext
.
get_executor
());
std
::
vector
<
int
>
v
;
h
.
setTimeout
(
[
&
v
,
&
h
]
{
v
.
push_back
(
500
);
h
.
stop
();
},
500
);
h
.
setTimeout
(
[
&
v
]
{
v
.
push_back
(
100
);
},
100
);
ioContext
.
run
();
REQUIRE
(
v
.
size
()
==
2
);
REQUIRE
(
v
[
0
]
==
100
);
REQUIRE
(
v
[
1
]
==
500
);
}
TEST_CASE
(
"setInterval should behave properly"
,
"[kazvjob]"
)
{
boost
::
asio
::
io_context
ioContext
;
CprJobHandler
h
(
ioContext
.
get_executor
());
std
::
vector
<
int
>
v
;
h
.
setInterval
(
[
&
v
,
&
h
]
{
v
.
push_back
(
50
);
std
::
cout
<<
"timer executed"
<<
std
::
endl
;
if
(
v
.
size
()
>=
2
)
{
h
.
stop
();
}
},
100
);
ioContext
.
run
();
REQUIRE
(
v
.
size
()
==
2
);
REQUIRE
(
v
[
0
]
==
50
);
REQUIRE
(
v
[
1
]
==
50
);
}
TEST_CASE
(
"setInterval can be cancelled"
,
"[kazvjob]"
)
{
boost
::
asio
::
io_context
ioContext
;
CprJobHandler
h
(
ioContext
.
get_executor
());
std
::
vector
<
int
>
v
;
std
::
string
id
{
"testTimerId"
};
h
.
setInterval
(
[
&
v
,
&
h
,
id
]
{
v
.
push_back
(
50
);
std
::
cout
<<
"timer executed"
<<
std
::
endl
;
if
(
v
.
size
()
>=
2
)
{
h
.
cancel
(
id
);
}
},
100
,
id
);
h
.
setTimeout
(
[
&
h
]
{
h
.
stop
();
},
300
);
ioContext
.
run
();
REQUIRE
(
v
.
size
()
==
2
);
REQUIRE
(
v
[
0
]
==
50
);
REQUIRE
(
v
[
1
]
==
50
);
}
static
BaseJob
succJob
=
BaseJob
(
TEST_SERVER_URL
,
"/.well-known/matrix/client"
,
BaseJob
::
Get
{},
"TestJob"
)
.
withQueue
(
"testjob"
);
static
BaseJob
failJob
=
BaseJob
(
TEST_SERVER_URL
,
"/.well-known/jfasjewfn/awejioaewjgjaad/fawre"
,
BaseJob
::
Get
{},
"AnotherTestJob"
)
.
withQueue
(
"testjob"
);
static
BaseJob
failJobWithCancel
=
failJob
.
withQueue
(
"testjob"
,
CancelFutureIfFailed
);
TEST_CASE
(
"Job queue should behave properly"
,
"[kazvjob]"
)
{
std
::
vector
<
bool
>
v
;
std
::
function
<
void
(
Response
)
>
callback
=
[
&
v
](
Response
r
)
{
std
::
cout
<<
"callback called"
<<
std
::
endl
;
v
.
push_back
(
r
.
success
());
};
boost
::
asio
::
io_context
ioContext
;
CprJobHandler
h
(
ioContext
.
get_executor
());
h
.
submit
(
succJob
,
callback
);
// true
h
.
submit
(
failJob
,
callback
);
// false
h
.
submit
(
failJobWithCancel
,
callback
);
// false
h
.
submit
(
succJob
,
callback
);
// false (because this is cancelled)
h
.
submit
(
succJob
,
[
&
h
](
Response
r
)
{
h
.
stop
();
});
// nothing added
ioContext
.
run
();
REQUIRE
(
v
.
size
()
==
4
);
REQUIRE
(
v
[
0
]
==
true
);
REQUIRE
(
v
[
1
]
==
false
);
REQUIRE
(
v
[
2
]
==
false
);
REQUIRE
(
v
[
3
]
==
false
);
}
static
const
std
::
string
httpbinServer
=
"http://www.httpbin.org"
;
static
const
std
::
string
httpbinEndpoint
=
"/post"
;
TEST_CASE
(
"Stream uploads should work properly"
,
"[kazvjob]"
)
{
auto
filename
=
std
::
filesystem
::
path
(
resPath
)
/
"kazvjob-test-res1"
;
auto
desc
=
FileDesc
(
filename
.
native
(),
"text/plain"
);
BaseJob
job
(
httpbinServer
,
httpbinEndpoint
,
BaseJob
::
POST
,
"TestJob"
,
std
::
string
{},
// token
BaseJob
::
ReturnType
::
Json
,
desc
);
boost
::
asio
::
io_context
ioContext
;
CprJobHandler
h
(
ioContext
.
get_executor
());
h
.
submit
(
job
,
[
&
h
](
Response
r
)
{
REQUIRE
(
isBodyJson
(
r
.
body
));
auto
body
=
r
.
jsonBody
();
std
::
cout
<<
"Ret= "
<<
body
.
get
().
dump
()
<<
std
::
endl
;
REQUIRE
(
body
.
get
().
at
(
"data"
).
template
get
<
std
::
string
>
()
==
"foobar"
);
REQUIRE
(
body
.
get
().
at
(
"headers"
).
at
(
"Content-Type"
).
template
get
<
std
::
string
>
()
==
"text/plain"
);
h
.
stop
();
});
ioContext
.
run
();
}
TEST_CASE
(
"Streaming binary data should be ok"
,
"[kazvjob]"
)
{
auto
filename
=
std
::
filesystem
::
path
(
resPath
)
/
"kazvjob-test-res2"
;
auto
desc
=
FileDesc
(
filename
.
native
(),
"application/octet-stream"
);
BaseJob
job
(
httpbinServer
,
httpbinEndpoint
,
BaseJob
::
POST
,
"TestJob"
,
std
::
string
{},
// token
BaseJob
::
ReturnType
::
Json
,
desc
);
boost
::
asio
::
io_context
ioContext
;
CprJobHandler
h
(
ioContext
.
get_executor
());
h
.
submit
(
job
,
[
&
h
](
Response
r
)
{
REQUIRE
(
isBodyJson
(
r
.
body
));
auto
body
=
r
.
jsonBody
();
std
::
cout
<<
"Ret= "
<<
body
.
get
().
dump
()
<<
std
::
endl
;
REQUIRE
(
body
.
get
().
at
(
"data"
).
template
get
<
std
::
string
>
()
==
"
\n\r\n\0\t\n
"
s
);
REQUIRE
(
body
.
get
().
at
(
"headers"
).
at
(
"Content-Type"
).
template
get
<
std
::
string
>
()
==
"application/octet-stream"
);
h
.
stop
();
});
ioContext
.
run
();
}
TEST_CASE
(
"Stream downloads should work properly"
,
"[kazvjob]"
)
{
auto
filename
=
std
::
filesystem
::
path
(
resPath
)
/
"kazvjob-test-tmp1"
;
auto
desc
=
FileDesc
(
filename
.
native
());
auto
guard
=
FileGuard
{
filename
};
BaseJob
job
(
httpbinServer
,
httpbinEndpoint
,
BaseJob
::
POST
,
"TestJob"
,
std
::
string
{},
// token
BaseJob
::
ReturnType
::
File
,
"foobar"
s
,
{},
// query
Header
(
Header
::
value_type
{{
"Content-Type"
,
"text/plain"
}}),
// header
desc
);
boost
::
asio
::
io_context
ioContext
;
CprJobHandler
h
(
ioContext
.
get_executor
());
h
.
submit
(
job
,
[
&
h
,
filename
](
Response
r
)
{
REQUIRE
(
std
::
holds_alternative
<
FileDesc
>
(
r
.
body
));
auto
body
=
std
::
get
<
FileDesc
>
(
r
.
body
);
REQUIRE
(
body
.
name
().
has_value
());
REQUIRE
(
body
.
name
().
value
()
==
filename
);
auto
fh
=
DumbFileInterface
{};
auto
stream
=
body
.
provider
(
fh
).
getStream
();
stream
.
read
(
1024
,
[](
auto
retCode
,
auto
data
)
{
REQUIRE
(
retCode
==
FileOpRetCode
::
Success
);
auto
j
=
json
::
parse
(
data
.
begin
(),
data
.
end
());
std
::
cout
<<
j
.
dump
()
<<
std
::
endl
;
REQUIRE
(
j
.
at
(
"data"
)
==
"foobar"
);
REQUIRE
(
j
.
at
(
"headers"
).
at
(
"Content-Type"
)
==
"text/plain"
);
});
h
.
stop
();
});
ioContext
.
run
();
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Tue, Jun 24, 6:49 PM (1 d, 2 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
234977
Default Alt Text
kazvjobtest.cpp (7 KB)
Attached To
Mode
rL libkazv
Attached
Detach File
Event Timeline
Log In to Comment