WillemJiang closed pull request #171: [SCB-300] acceptance test of the timeout URL: https://github.com/apache/incubator-servicecomb-saga/pull/171
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/acceptance-tests/acceptance-pack/src/test/java/org/apache/servicecomb/saga/PackStepdefs.java b/acceptance-tests/acceptance-pack/src/test/java/org/apache/servicecomb/saga/PackStepdefs.java index e256be93..857bb9f5 100644 --- a/acceptance-tests/acceptance-pack/src/test/java/org/apache/servicecomb/saga/PackStepdefs.java +++ b/acceptance-tests/acceptance-pack/src/test/java/org/apache/servicecomb/saga/PackStepdefs.java @@ -26,6 +26,7 @@ import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -51,6 +52,8 @@ private static final Consumer<Map<String, String>[]> NO_OP_CONSUMER = (dataMap) -> { }; + private static final Map<String, Submit> submits = new HashMap<>(); + public PackStepdefs() { Given("^Car Service is up and running$", () -> { probe(System.getProperty(CAR_SERVICE_ADDRESS)); @@ -69,12 +72,10 @@ public PackStepdefs() { }); Given("^Install the byteman script ([A-Za-z0-9_\\.]+) to ([A-Za-z]+) Service$", (String script, String service) -> { - String address = System.getProperty("byteman.address"); - String port = System.getProperty(service.toLowerCase() + ".byteman.port"); - log.info("Install the byteman script {} to {} service with {}:{}", script, service, address, port); - Submit bm = new Submit(address, Integer.parseInt(port)); + log.info("Install the byteman script {} to {} service", script, service); List<String> rules = new ArrayList<>(); rules.add("target/test-classes/" + script); + Submit bm = getBytemanSubmit(service); bm.addRulesFromFiles(rules); }); @@ -123,6 +124,14 @@ public void cleanUp() { .delete(System.getProperty(ALPHA_REST_ADDRESS) + "/events") .then() .statusCode(is(200)); + + for (Submit bm : submits.values()) { + try { + bm.deleteAllRules(); + } catch (Exception e) { + log.warn("Fail to delete the byteman rules " + e); + } + } } @SuppressWarnings("unchecked") @@ -178,4 +187,15 @@ private void probe(String address, String infoURI) { .statusCode(is(200)); } + private Submit getBytemanSubmit(String service) { + if (submits.containsKey(service)) { + return submits.get(service); + } else { + String address = System.getProperty("byteman.address"); + String port = System.getProperty(service.toLowerCase() + ".byteman.port"); + Submit bm = new Submit(address, Integer.parseInt(port)); + submits.put(service, bm); + return bm; + } + } } diff --git a/acceptance-tests/acceptance-pack/src/test/resources/booking_timeout.btm b/acceptance-tests/acceptance-pack/src/test/resources/booking_timeout.btm new file mode 100644 index 00000000..efc69029 --- /dev/null +++ b/acceptance-tests/acceptance-pack/src/test/resources/booking_timeout.btm @@ -0,0 +1,13 @@ +############################################################## +# rules to timeout the booking after invoking the services +# +############################################################### + +RULE sleep when postBooking until timeout happens +CLASS org.apache.servicecomb.saga.demo.pack.booking.BookingController +METHOD postBooking +AT ENTRY +IF TRUE +DO debug("delay 10s until the booking timeout"), + Thread.sleep(10000) +ENDRULE diff --git a/acceptance-tests/acceptance-pack/src/test/resources/pack_timeout_scenario.feature b/acceptance-tests/acceptance-pack/src/test/resources/pack_timeout_scenario.feature new file mode 100644 index 00000000..2a3e59de --- /dev/null +++ b/acceptance-tests/acceptance-pack/src/test/resources/pack_timeout_scenario.feature @@ -0,0 +1,47 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +Feature: Alpha records transaction events + + Scenario: A transaction timeout and will be compensated + Given Car Service is up and running + And Hotel Service is up and running + And Booking Service is up and running + And Alpha is up and running + + Given Install the byteman script booking_timeout.btm to Booking Service + + When User Sean requests to book 1 cars and 1 rooms + + Then Alpha records the following events + | serviceName | type | + | pack-booking | SagaStartedEvent | + | pack-car | TxStartedEvent | + | pack-car | TxEndedEvent | + | pack-hotel | TxStartedEvent | + | pack-hotel | TxEndedEvent | + | pack-booking | TxAbortedEvent | + | pack-hotel | TxCompensatedEvent | + | pack-car | TxCompensatedEvent | + | pack-booking | SagaEndedEvent | + + Then Car Service contains the following booking orders + | name | amount | confirmed | cancelled | + | Sean | 1 | false | true | + + Then Hotel Service contains the following booking orders + | name | amount | confirmed | cancelled | + | Sean | 1 | false | true | + diff --git a/saga-demo/booking/booking/src/main/java/org/apache/servicecomb/saga/demo/pack/booking/BookingController.java b/saga-demo/booking/booking/src/main/java/org/apache/servicecomb/saga/demo/pack/booking/BookingController.java index f580a58a..3d6c2ce5 100644 --- a/saga-demo/booking/booking/src/main/java/org/apache/servicecomb/saga/demo/pack/booking/BookingController.java +++ b/saga-demo/booking/booking/src/main/java/org/apache/servicecomb/saga/demo/pack/booking/BookingController.java @@ -37,7 +37,7 @@ @Autowired private RestTemplate template; - @SagaStart + @SagaStart(timeout = 5) @PostMapping("/booking/{name}/{rooms}/{cars}") public String order(@PathVariable String name, @PathVariable Integer rooms, @PathVariable Integer cars) { template.postForEntity( @@ -48,6 +48,13 @@ public String order(@PathVariable String name, @PathVariable Integer rooms, @Pa hotelServiceUrl + "/order/{name}/{rooms}", null, String.class, name, rooms); + postBooking(); + return name + " booking " + rooms + " rooms and " + cars + " cars OK"; } + + // This method is used by the byteman to inject the faults such as the timeout or the crash + private void postBooking() { + + } } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services