A common pattern in tests is to use try-finally to make sure
cleanup is executed. This is necessary in CTS in particular to
make sure the test does not leave the device in a bad state.
The problem with using try-finally in this manner is that any
exception thrown in the finally{} block will override any thrown
in the try{} block. If the exception in finally{} is caused by
the one from try{}, then the stack trace reported by the tools
is the consequence and not the cause, making it difficult to
interpret the stack trace of automated tests.
So today, code has to make sure that either the code in
finally{} can't throw, or can't be affected by any code in
try{}, and both of these are really difficult to ensure in
presence in finally{} of code the tester does not control.
What we'd want ideally is a structure like try-finally, that
guaratees the code in finally{} is executed in all cases,
but that bubbles up the exception from try{} if any, and
will still bubble up any exception from finally{} if the
try{} block hasn't thrown.
That's what this new tool does.
Usage from Kotlin is like try-finally :
tryTest {
testing code
} cleanup {
cleanup code
}
Usage from Java can't be made as nice, but it's relatively okay :
testAndCleanup(() -> {
testing code
}, () -> {
cleanup code
});
Bugs listed below are some tests that have been affected by
this issue and have unhelpful traces.
Test: new test for this code
Bug: 198586720
Bug: 198998862
Change-Id: I54b30a7d53772feeade99274b6120a79707ad1c9
60 lines
1.7 KiB
Text
60 lines
1.7 KiB
Text
// Copyright (C) 2021 The Android Open Source Project
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package {
|
|
default_applicable_licenses: ["Android-Apache-2.0"],
|
|
}
|
|
|
|
java_library {
|
|
name: "net-tests-utils",
|
|
srcs: [
|
|
"devicetests/**/*.java",
|
|
"devicetests/**/*.kt",
|
|
],
|
|
defaults: ["lib_mockito_extended"],
|
|
libs: [
|
|
"androidx.annotation_annotation",
|
|
],
|
|
static_libs: [
|
|
"androidx.test.ext.junit",
|
|
"kotlin-reflect",
|
|
"libnanohttpd",
|
|
"net-tests-utils-host-device-common",
|
|
"net-utils-device-common",
|
|
"net-utils-device-common-netlink",
|
|
"modules-utils-build_system",
|
|
],
|
|
}
|
|
|
|
java_library {
|
|
// Consider using net-tests-utils instead if writing device code.
|
|
// That library has a lot more useful tools into it for users that
|
|
// work on Android and includes this lib.
|
|
name: "net-tests-utils-host-device-common",
|
|
srcs: [
|
|
"hostdevice/**/*.java",
|
|
"hostdevice/**/*.kt",
|
|
],
|
|
host_supported: true,
|
|
visibility: [
|
|
"//frameworks/libs/net/common/tests:__subpackages__",
|
|
"//frameworks/libs/net/client-libs/tests:__subpackages__",
|
|
],
|
|
libs: [
|
|
"jsr305",
|
|
],
|
|
static_libs: [
|
|
"kotlin-test"
|
|
]
|
|
}
|