Skip to content

Commit 30f9d4a

Browse files
authored
feat!: Rework globals mechanism to better support shared resources (#70)
1 parent 7936d88 commit 30f9d4a

21 files changed

+821
-236
lines changed

.github/workflows/unit.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ jobs:
1313
strategy:
1414
matrix:
1515
include:
16-
- os: ubuntu-latest
17-
ruby: "2.4"
18-
flags: "--only --test-unit"
1916
- os: ubuntu-latest
2017
ruby: "2.5"
2118
flags: "--only --test-unit"

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ inherit_gem:
22
google-style: google-style.yml
33

44
AllCops:
5+
TargetRubyVersion: 2.5
56
Include:
67
- "examples/**/.toys.rb"
78
- "**/*.rb"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ requiring an HTTP server or complicated request handling logic.
4242

4343
## Supported Ruby versions
4444

45-
This library is supported on Ruby 2.4+.
45+
This library is supported on Ruby 2.5+.
4646

4747
Google provides official support for Ruby versions that are actively supported
4848
by Ruby Core—that is, Ruby versions that are either in normal maintenance or
49-
in security maintenance, and not end of life. Currently, this means Ruby 2.4
49+
in security maintenance, and not end of life. Currently, this means Ruby 2.5
5050
and later. Older versions of Ruby _may_ still work, but are unsupported and not
5151
recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
5252
about the Ruby support schedule.

docs/overview.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ requiring an HTTP server or complicated request handling logic.
4646

4747
## Supported Ruby versions
4848

49-
This library is supported on Ruby 2.4+.
49+
This library is supported on Ruby 2.5+.
5050

5151
Google provides official support for Ruby versions that are actively supported
5252
by Ruby Core—that is, Ruby versions that are either in normal maintenance or
53-
in security maintenance, and not end of life. Currently, this means Ruby 2.4
53+
in security maintenance, and not end of life. Currently, this means Ruby 2.5
5454
and later. Older versions of Ruby _may_ still work, but are unsupported and not
5555
recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
5656
about the Ruby support schedule.
@@ -64,7 +64,7 @@ Create a `Gemfile` listing the Functions Framework as a dependency:
6464
```ruby
6565
# Gemfile
6666
source "https://rubygems.org"
67-
gem "functions_framework", "~> 0.5"
67+
gem "functions_framework", "~> 0.7"
6868
```
6969

7070
Create a file called `app.rb` and include the following code. This defines a

docs/testing-functions.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,53 @@ class MyTest < Minitest::Test
165165
end
166166
end
167167
```
168+
169+
## Testing startup tasks
170+
171+
When a functions server is starting up, it calls startup tasks automatically.
172+
In the testing environment, when you call a function using the
173+
{FunctionsFramework::Testing#call_http} or
174+
{FunctionsFramework::Testing#call_event} methods, the testing environment will
175+
also automatically execute any startup tasks for you.
176+
177+
You can also call startup tasks explicitly to test them in isolation, using the
178+
{FunctionsFramework::Testing#run_startup_tasks} method. Pass the name of a
179+
function, and the testing module will execute all defined startup blocks, in
180+
order, as if the server were preparing that function for execution.
181+
{FunctionsFramework::Testing#run_startup_tasks} returns the resulting globals
182+
as a hash, so you can assert against its contents.
183+
184+
If you use {FunctionsFramework::Testing#run_startup_tasks} to run the startup
185+
tasks explicitly, they will not be run again when you call the function itself
186+
using {FunctionsFramework::Testing#call_http} or
187+
{FunctionsFramework::Testing#call_event}. However, if startup tasks have
188+
already been run implicitly by {FunctionsFramework::Testing#call_http} or
189+
{FunctionsFramework::Testing#call_event}, then attempting to run them again
190+
explicitly by calling {FunctionsFramework::Testing#run_startup_tasks} will
191+
result in an exception.
192+
193+
There is currently no way to run a single startup block in isolation. If you
194+
have multiple startup blocks defined, they are always executed together.
195+
196+
Following is an example test that runs startup tasks explicitly and asserts
197+
against the effect on the globals.
198+
199+
```ruby
200+
require "minitest/autorun"
201+
require "functions_framework/testing"
202+
203+
class MyTest < Minitest::Test
204+
include FunctionsFramework::Testing
205+
206+
def test_startup_tasks
207+
load_temporary "app.rb" do
208+
globals = run_startup_tasks "my_function"
209+
assert_equal "foo", globals[:my_global]
210+
211+
request = make_get_request "https://example.com/foo"
212+
response = call_http "my_function", request
213+
assert_equal 200, response.status
214+
end
215+
end
216+
end
217+
```

0 commit comments

Comments
 (0)