Skip to content

Commit

Permalink
add tests for timeout handling (#74)
Browse files Browse the repository at this point in the history
Adding some tests for timeout handling. Also tests the use of `OpenAPI.Clients.with_timeout` method provided to invoke OpenAPI client side APIs with a timeout value.
  • Loading branch information
tanmaykm authored Apr 18, 2024
1 parent 6c820c5 commit 512cb75
Show file tree
Hide file tree
Showing 26 changed files with 647 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.openapi-generator-ignore
README.md
docs/DefaultApi.md
docs/DelayresponseGet200Response.md
src/TimeoutTestClient.jl
src/apis/api_DefaultApi.jl
src/modelincludes.jl
src/models/model_DelayresponseGet200Response.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.2.0
39 changes: 39 additions & 0 deletions test/client/timeouttest/TimeoutTestClient/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Julia API client for TimeoutTestClient

No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

## Overview
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [openapi-spec](https://openapis.org) from a remote server, you can easily generate an API client.

- API version: 1.0.0
- Build package: org.openapitools.codegen.languages.JuliaClientCodegen


## Installation
Place the Julia files generated under the `src` folder in your Julia project. Include TimeoutTestClient.jl in the project code.
It would include the module named TimeoutTestClient.

Documentation is generated as markdown files under the `docs` folder. You can include them in your project documentation.
Documentation is also embedded in Julia which can be used with a Julia specific documentation generator.

## API Endpoints

Class | Method
------------ | -------------
*DefaultApi* | [**delayresponse_get**](docs/DefaultApi.md#delayresponse_get)<br/>**GET** /delayresponse<br/>Delay Response Endpoint


## Models

- [DelayresponseGet200Response](docs/DelayresponseGet200Response.md)


<a id="authorization"></a>
## Authorization
Endpoints do not require authorization.


## Author



37 changes: 37 additions & 0 deletions test/client/timeouttest/TimeoutTestClient/docs/DefaultApi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# DefaultApi

All URIs are relative to *http://localhost*

Method | HTTP request | Description
------------- | ------------- | -------------
[**delayresponse_get**](DefaultApi.md#delayresponse_get) | **GET** /delayresponse | Delay Response Endpoint


# **delayresponse_get**
> delayresponse_get(_api::DefaultApi, delay_seconds::Int64; _mediaType=nothing) -> DelayresponseGet200Response, OpenAPI.Clients.ApiResponse <br/>
> delayresponse_get(_api::DefaultApi, response_stream::Channel, delay_seconds::Int64; _mediaType=nothing) -> Channel{ DelayresponseGet200Response }, OpenAPI.Clients.ApiResponse
Delay Response Endpoint

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **DefaultApi** | API context |
**delay_seconds** | **Int64**| Number of seconds to delay the response | [default to nothing]

### Return type

[**DelayresponseGet200Response**](DelayresponseGet200Response.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#api-endpoints) [[Back to Model list]](../README.md#models) [[Back to README]](../README.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# DelayresponseGet200Response


## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**delay_seconds** | **String** | | [optional] [default to nothing]


[[Back to Model list]](../README.md#models) [[Back to API list]](../README.md#api-endpoints) [[Back to README]](../README.md)


16 changes: 16 additions & 0 deletions test/client/timeouttest/TimeoutTestClient/src/TimeoutTestClient.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file was generated by the Julia OpenAPI Code Generator
# Do not modify this file directly. Modify the OpenAPI specification instead.

module TimeoutTestClient

using Dates, TimeZones
using OpenAPI
using OpenAPI.Clients

const API_VERSION = "1.0.0"

include("modelincludes.jl")

include("apis/api_DefaultApi.jl")

end # module TimeoutTestClient
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file was generated by the Julia OpenAPI Code Generator
# Do not modify this file directly. Modify the OpenAPI specification instead.

struct DefaultApi <: OpenAPI.APIClientImpl
client::OpenAPI.Clients.Client
end

"""
The default API base path for APIs in `DefaultApi`.
This can be used to construct the `OpenAPI.Clients.Client` instance.
"""
basepath(::Type{ DefaultApi }) = "http://localhost"

const _returntypes_delayresponse_get_DefaultApi = Dict{Regex,Type}(
Regex("^" * replace("200", "x"=>".") * "\$") => DelayresponseGet200Response,
)

function _oacinternal_delayresponse_get(_api::DefaultApi, delay_seconds::Int64; _mediaType=nothing)
OpenAPI.validate_param("delay_seconds", "delayresponse_get", :minimum, delay_seconds, 0, false)

_ctx = OpenAPI.Clients.Ctx(_api.client, "GET", _returntypes_delayresponse_get_DefaultApi, "/delayresponse", [])
OpenAPI.Clients.set_param(_ctx.query, "delay_seconds", delay_seconds) # type Int64
OpenAPI.Clients.set_header_accept(_ctx, ["application/json", ])
OpenAPI.Clients.set_header_content_type(_ctx, (_mediaType === nothing) ? [] : [_mediaType])
return _ctx
end

@doc raw"""Delay Response Endpoint
Params:
- delay_seconds::Int64 (required)
Return: DelayresponseGet200Response, OpenAPI.Clients.ApiResponse
"""
function delayresponse_get(_api::DefaultApi, delay_seconds::Int64; _mediaType=nothing)
_ctx = _oacinternal_delayresponse_get(_api, delay_seconds; _mediaType=_mediaType)
return OpenAPI.Clients.exec(_ctx)
end

function delayresponse_get(_api::DefaultApi, response_stream::Channel, delay_seconds::Int64; _mediaType=nothing)
_ctx = _oacinternal_delayresponse_get(_api, delay_seconds; _mediaType=_mediaType)
return OpenAPI.Clients.exec(_ctx, response_stream)
end

export delayresponse_get
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file was generated by the Julia OpenAPI Code Generator
# Do not modify this file directly. Modify the OpenAPI specification instead.

include("models/model_DelayresponseGet200Response.jl")
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file was generated by the Julia OpenAPI Code Generator
# Do not modify this file directly. Modify the OpenAPI specification instead.


@doc raw"""_delayresponse_get_200_response
DelayresponseGet200Response(;
delay_seconds=nothing,
)
- delay_seconds::String
"""
Base.@kwdef mutable struct DelayresponseGet200Response <: OpenAPI.APIModel
delay_seconds::Union{Nothing, String} = nothing

function DelayresponseGet200Response(delay_seconds, )
OpenAPI.validate_property(DelayresponseGet200Response, Symbol("delay_seconds"), delay_seconds)
return new(delay_seconds, )
end
end # type DelayresponseGet200Response

const _property_types_DelayresponseGet200Response = Dict{Symbol,String}(Symbol("delay_seconds")=>"String", )
OpenAPI.property_type(::Type{ DelayresponseGet200Response }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_DelayresponseGet200Response[name]))}

function check_required(o::DelayresponseGet200Response)
true
end

function OpenAPI.validate_property(::Type{ DelayresponseGet200Response }, name::Symbol, val)
end
5 changes: 5 additions & 0 deletions test/client/timeouttest/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
java -jar openapi-generator-cli.jar generate \
-i ../../specs/timeouttest.yaml \
-g julia-client \
-o TimeoutTestClient \
--additional-properties=packageName=TimeoutTestClient
58 changes: 58 additions & 0 deletions test/client/timeouttest/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module TimeoutTests

include(joinpath(@__DIR__, "TimeoutTestClient", "src", "TimeoutTestClient.jl"))
using .TimeoutTestClient
using Test
using JSON
using HTTP
using OpenAPI
using OpenAPI.Clients
import OpenAPI.Clients: Client, with_timeout, ApiException

const M = TimeoutTestClient
const server = "http://127.0.0.1:8081"

function test_normal_operation(client, delay_secs)
@info("timeout default, delay $delay_secs secs")
api = M.DefaultApi(client)
api_return, http_resp = delayresponse_get(api, delay_secs)
@test http_resp.status == 200
@test api_return.delay_seconds == string(delay_secs)
end

function test_timeout_operation(client, timeout_secs, delay_secs)
@info("timeout $timeout_secs secs, delay $delay_secs secs")
with_timeout(client, timeout_secs) do client
try
api = M.DefaultApi(client)
delayresponse_get(api, delay_secs)
error("Timeout not thrown")
catch ex
@test isa(ex, ApiException)
@test ex.status == 0
@test startswith(ex.reason, "Operation timed out")
end
end
end

function runtests()
@testset "timeout_tests" begin
@info("TimeoutTest")
client = Client(server)

test_normal_operation(client, 10)

for timeout_secs in (5, 120) # test different timeouts
delay_secs = timeout_secs + 60
test_timeout_operation(client, timeout_secs, delay_secs)

# but the client should still be usable
test_normal_operation(client, 10)
end

# also test a long delay in general (default libcurl timeout is 0)
test_normal_operation(client, 160)
end
end

end # module TimeoutTests
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include("modelgen/testmodelgen.jl")
include("client/runtests.jl")
include("client/allany/runtests.jl")
include("forms/forms_client.jl")
include("client/timeouttest/runtests.jl")

@testset "OpenAPI" begin
@testset "ModelGen" begin
Expand Down Expand Up @@ -153,4 +154,28 @@ include("forms/forms_client.jl")
@testset "Helper Methods" begin
AllAnyTests.test_http_resp()
end

@testset "Timeout Handling" begin
ret = out = nothing
servers_running = true

try
if run_tests_with_servers
ret, out = run_server(joinpath(@__DIR__, "server", "timeouttest", "timeouttest_server.jl"))
servers_running &= wait_server(8081)
TimeoutTests.runtests()
else
servers_running = false
end
finally
if run_tests_with_servers && !servers_running
# we probably had an error starting the servers
out_str = isnothing(out) ? "" : String(take!(out))
@warn("Servers not running", ret=ret, out_str)
end
run_tests_with_servers && stop_server(8081, ret, out)
end
end
run_tests_with_servers && sleep(20) # avoid port conflicts

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.openapi-generator-ignore
README.md
docs/DefaultApi.md
docs/DelayresponseGet200Response.md
src/TimeoutTestServer.jl
src/apis/api_DefaultApi.jl
src/modelincludes.jl
src/models/model_DelayresponseGet200Response.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.2.0
Loading

0 comments on commit 512cb75

Please sign in to comment.