Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
polkadot-sdk
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
Mirrored projects
polkadot-sdk
Commits
ba98168b
Commit
ba98168b
authored
5 years ago
by
Bastian Köcher
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Make `TestOffchainExt` panic on unknown requests (#3710)
* Make `TestOffchainExt` panic on unknown requests * Fix test
parent
0da34de3
Branches
gh-readonly-queue/master/pr-6738-fd64a1e7768ba6e8676cbbf25c4e821a901c0a7f
Branches containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
substrate/core/executor/src/wasm_executor.rs
+1
-1
1 addition, 1 deletion
substrate/core/executor/src/wasm_executor.rs
substrate/core/offchain/src/testing.rs
+15
-7
15 additions, 7 deletions
substrate/core/offchain/src/testing.rs
with
16 additions
and
8 deletions
substrate/core/executor/src/wasm_executor.rs
+
1
−
1
View file @
ba98168b
...
@@ -1842,7 +1842,7 @@ mod tests {
...
@@ -1842,7 +1842,7 @@ mod tests {
body
:
vec!
[
1
,
2
,
3
,
4
],
body
:
vec!
[
1
,
2
,
3
,
4
],
headers
:
vec!
[(
"X-Auth"
.to_owned
(),
"test"
.to_owned
())],
headers
:
vec!
[(
"X-Auth"
.to_owned
(),
"test"
.to_owned
())],
sent
:
true
,
sent
:
true
,
response
:
vec!
[
1
,
2
,
3
],
response
:
Some
(
vec!
[
1
,
2
,
3
]
)
,
response_headers
:
vec!
[(
"X-Auth"
.to_owned
(),
"hello"
.to_owned
())],
response_headers
:
vec!
[(
"X-Auth"
.to_owned
(),
"hello"
.to_owned
())],
..
Default
::
default
()
..
Default
::
default
()
},
},
...
...
This diff is collapsed.
Click to expand it.
substrate/core/offchain/src/testing.rs
+
15
−
7
View file @
ba98168b
...
@@ -48,7 +48,7 @@ pub struct PendingRequest {
...
@@ -48,7 +48,7 @@ pub struct PendingRequest {
/// Has the request been sent already.
/// Has the request been sent already.
pub
sent
:
bool
,
pub
sent
:
bool
,
/// Response body
/// Response body
pub
response
:
Vec
<
u8
>
,
pub
response
:
Option
<
Vec
<
u8
>
>
,
/// Number of bytes already read from the response body.
/// Number of bytes already read from the response body.
pub
read
:
usize
,
pub
read
:
usize
,
/// Response headers
/// Response headers
...
@@ -89,7 +89,7 @@ impl State {
...
@@ -89,7 +89,7 @@ impl State {
*
req
,
*
req
,
expected
,
expected
,
);
);
req
.response
=
response
.into
();
req
.response
=
Some
(
response
.into
()
)
;
req
.response_headers
=
response_headers
.into_iter
()
.collect
();
req
.response_headers
=
response_headers
.into_iter
()
.collect
();
}
}
}
}
...
@@ -97,7 +97,7 @@ impl State {
...
@@ -97,7 +97,7 @@ impl State {
fn
fulfill_expected
(
&
mut
self
,
id
:
u16
)
{
fn
fulfill_expected
(
&
mut
self
,
id
:
u16
)
{
if
let
Some
(
mut
req
)
=
self
.expected_requests
.remove
(
&
RequestId
(
id
))
{
if
let
Some
(
mut
req
)
=
self
.expected_requests
.remove
(
&
RequestId
(
id
))
{
let
response
=
std
::
mem
::
replace
(
&
mut
req
.response
,
vec!
[]
);
let
response
=
req
.response
.take
()
.expect
(
"Response checked while added."
);
let
headers
=
std
::
mem
::
replace
(
&
mut
req
.response_headers
,
vec!
[]);
let
headers
=
std
::
mem
::
replace
(
&
mut
req
.response_headers
,
vec!
[]);
self
.fulfill_pending_request
(
id
,
req
,
response
,
headers
);
self
.fulfill_pending_request
(
id
,
req
,
response
,
headers
);
}
}
...
@@ -110,6 +110,9 @@ impl State {
...
@@ -110,6 +110,9 @@ impl State {
/// Expected request has to be fulfilled before this struct is dropped,
/// Expected request has to be fulfilled before this struct is dropped,
/// the `response` and `response_headers` fields will be used to return results to the callers.
/// the `response` and `response_headers` fields will be used to return results to the callers.
pub
fn
expect_request
(
&
mut
self
,
id
:
u16
,
expected
:
PendingRequest
)
{
pub
fn
expect_request
(
&
mut
self
,
id
:
u16
,
expected
:
PendingRequest
)
{
if
expected
.response
.is_none
()
{
panic!
(
"Expected request needs to have a response."
);
}
self
.expected_requests
.insert
(
RequestId
(
id
),
expected
);
self
.expected_requests
.insert
(
RequestId
(
id
),
expected
);
}
}
}
}
...
@@ -254,7 +257,8 @@ impl offchain::Externalities for TestOffchainExt {
...
@@ -254,7 +257,8 @@ impl offchain::Externalities for TestOffchainExt {
let
state
=
self
.0
.read
();
let
state
=
self
.0
.read
();
ids
.iter
()
.map
(|
id
|
match
state
.requests
.get
(
id
)
{
ids
.iter
()
.map
(|
id
|
match
state
.requests
.get
(
id
)
{
Some
(
req
)
if
req
.response
.is_empty
()
=>
RequestStatus
::
DeadlineReached
,
Some
(
req
)
if
req
.response
.is_none
()
=>
panic!
(
"No `response` provided for request with id: {:?}"
,
id
),
None
=>
RequestStatus
::
Invalid
,
None
=>
RequestStatus
::
Invalid
,
_
=>
RequestStatus
::
Finished
(
200
),
_
=>
RequestStatus
::
Finished
(
200
),
})
.collect
()
})
.collect
()
...
@@ -281,13 +285,17 @@ impl offchain::Externalities for TestOffchainExt {
...
@@ -281,13 +285,17 @@ impl offchain::Externalities for TestOffchainExt {
)
->
Result
<
usize
,
HttpError
>
{
)
->
Result
<
usize
,
HttpError
>
{
let
mut
state
=
self
.0
.write
();
let
mut
state
=
self
.0
.write
();
if
let
Some
(
req
)
=
state
.requests
.get_mut
(
&
request_id
)
{
if
let
Some
(
req
)
=
state
.requests
.get_mut
(
&
request_id
)
{
if
req
.read
>=
req
.response
.len
()
{
let
response
=
req
.response
.as_mut
()
.expect
(
&
format!
(
"No response provided for request: {:?}"
,
request_id
));
if
req
.read
>=
response
.len
()
{
// Remove the pending request as per spec.
// Remove the pending request as per spec.
state
.requests
.remove
(
&
request_id
);
state
.requests
.remove
(
&
request_id
);
Ok
(
0
)
Ok
(
0
)
}
else
{
}
else
{
let
read
=
std
::
cmp
::
min
(
buffer
.len
(),
req
.
response
[
req
.read
..
]
.len
());
let
read
=
std
::
cmp
::
min
(
buffer
.len
(),
response
[
req
.read
..
]
.len
());
buffer
[
0
..
read
]
.copy_from_slice
(
&
req
.
response
[
req
.read
..
read
]);
buffer
[
0
..
read
]
.copy_from_slice
(
&
response
[
req
.read
..
read
]);
req
.read
+=
read
;
req
.read
+=
read
;
Ok
(
read
)
Ok
(
read
)
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment