-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve java matter controller example result check (#24311)
-- Introduce realReusult and futureResult where we use wait and notify to check whether expected result is received. -- Replace the success/failure words with boolean and error string.
- Loading branch information
1 parent
2d2c7b1
commit 2110354
Showing
6 changed files
with
140 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...s/java-matter-controller/java/src/com/matter/controller/commands/common/FutureResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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 com.matter.controller.commands.common; | ||
|
||
import java.util.Optional; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Implements the future result that encapculates the optional realResult, application would wait | ||
* for realResult set by other thread wben receiving data from the other end. If the expected | ||
* duration elapsed without receiving the expected realResult, the runtime exception would be | ||
* raised. | ||
*/ | ||
public class FutureResult { | ||
private Optional<RealResult> realResult; | ||
private long timeoutMs = 0; | ||
private static Logger logger = Logger.getLogger(FutureResult.class.getName()); | ||
|
||
public void setTimeoutMs(long timeoutMs) { | ||
this.timeoutMs = timeoutMs; | ||
} | ||
|
||
public synchronized void setRealResult(RealResult realResult) { | ||
if (this.realResult.isPresent()) { | ||
throw new RuntimeException("error, real result has been set!"); | ||
} | ||
this.realResult = Optional.of(realResult); | ||
notifyAll(); | ||
} | ||
|
||
public synchronized void waitResult() { | ||
long start = System.currentTimeMillis(); | ||
while (!realResult.isPresent()) { | ||
try { | ||
if (System.currentTimeMillis() > (start + timeoutMs)) { | ||
throw new RuntimeException("timeout!"); | ||
} | ||
wait(); | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
|
||
if (!realResult.get().getResult()) { | ||
logger.log(Level.INFO, "error: %s", realResult.get().getError()); | ||
throw new RuntimeException("received failure test result"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...les/java-matter-controller/java/src/com/matter/controller/commands/common/RealResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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 com.matter.controller.commands.common; | ||
|
||
/** | ||
* Implements a Result where an error string is associated with its failure state. | ||
* | ||
* <p>A `Result` is just a booean of true/false that is exposed via `getResult`. This class will | ||
* contain either a `true` value for `Success` or a `false` value in which case the failure will | ||
* also have an error string explaining the reason of the failure associated with it. | ||
*/ | ||
public class RealResult { | ||
private boolean result; | ||
private String error; | ||
|
||
public RealResult() { | ||
this.result = true; | ||
} | ||
|
||
public RealResult(String error) { | ||
this.result = false; | ||
this.error = error; | ||
} | ||
|
||
public static RealResult Success() { | ||
return new RealResult(); | ||
} | ||
|
||
public static RealResult Error(String error) { | ||
return new RealResult(error); | ||
} | ||
|
||
public boolean getResult() { | ||
return result; | ||
} | ||
|
||
public String getError() { | ||
return error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters