Skip to content

Commit 84eed8f

Browse files
committed
Add local testing guide
1 parent d831745 commit 84eed8f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,41 @@ The library’s behavior can be fine tuned using environment variables based con
324324
* `STOP_SIGNAL`: Signal to capture for termination. Set to TERM by default.
325325
* `REQUEST_TIMEOUT`: Max time to wait for responses to come back from the SCF runtime engine. Set to none by default.
326326

327+
### Local Testing (DEBUG Mode Only)
328+
329+
If you'd like to test the cloud function with HTTP requests, we provide a simple local emulator. To enable it, set the environment variable `LOCAL_SCF_SERVER_ENABLED` to `true`.
330+
331+
By `swift run`, you'll see the logger output saying `LocalSCFServer started and listening on 127.0.0.1:9001, receiving events on /invoke` which means the local emulator is up. Then you can send HTTP `POST` requests to `http://127.0.0.1:9001/invoke` endpoint to invoke your cloud function.
332+
333+
We also provide a way to inject environment variables without actually changing it. Since it only works in DEBUG mode, don't forget to wrap such codes with `#if DEBUG` and `#endif`.
334+
335+
```swift
336+
// Import the module.
337+
import TencentSCFRuntime
338+
339+
#if DEBUG
340+
// Start LocalServer by default.
341+
SCF.Env["LOCAL_SCF_SERVER_ENABLED"] = "true"
342+
343+
// Simulate SCF environment.
344+
let variables = [
345+
"TENCENTCLOUD_UIN": "100012345678",
346+
"TENCENTCLOUD_APPID": "123456789",
347+
"TENCENTCLOUD_REGION": "ap-shanghai"
348+
]
349+
SCF.Env.update(with: variables)
350+
#endif
351+
352+
// Our SCF handler.
353+
SCF.run { (context, name: String, callback: @escaping (Result<String, Error>) -> Void) in
354+
...
355+
}
356+
```
357+
358+
We simulate the SCF environment with some variables set by default. The value set by user code is of the highest priority, while the framework simulation has the lowest.
359+
360+
You can read some contextual variables through `SCF.Context`. All the environment variables can be accessed through `SCF.Env`.
361+
327362
### SCF Runtime Engine Integration
328363

329364
The library is designed to integrate with SCF Runtime Engine via the [SCF Custom Runtime API](https://cloud.tencent.com/document/product/583/47274#custom-runtime-.E8.BF.90.E8.A1.8C.E6.97.B6-api) which was introduced as part of [SCF Custom Runtime](https://cloud.tencent.com/document/product/583/47274) in 2020. The latter is an HTTP server that exposes three main RESTful endpoint:

0 commit comments

Comments
 (0)