Skip to content

Commit 8a5c184

Browse files
jlucktaymigueleliasweb
authored andcommitted
docs(readme): add a section on mocking rate limits
1 parent abf52fe commit 8a5c184

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

README.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33

44
A library to aid unittesting code that uses Golang's Github SDK
55

6-
# Installation
6+
## Installation
77

88
```bash
99
go get github.com/migueleliasweb/go-github-mock
1010
```
1111

12-
# Features
12+
## Features
1313

1414
- Create mocks for successive calls for the same endpoint
1515
- Pagination support
1616
- Mock error returns
1717
- High level abstraction helps writing readabe unittests (see `mock.WithRequestMatch`)
1818
- Lower level abstraction for advanced uses (see `mock.WithRequestMatchHandler`)
1919

20-
# Breaking changes
20+
## Breaking changes
2121

2222
- `v0.0.3` the API for the server options have beem simplified
2323
- `v0.0.4` fixes to the gen script caused multiple url matches to change
2424

25-
# Example
25+
## Examples
2626

27-
```
27+
```go
2828
import "github.com/migueleliasweb/go-github-mock/src/mock"
2929
```
3030

31-
## Multiple requests
31+
### Multiple requests
3232

3333
```golang
3434
mockedHTTPClient := mock.NewMockedHTTPClient(
@@ -87,7 +87,7 @@ projs, _, projsErr := c.Organizations.ListProjects(
8787

8888
```
8989

90-
## Returning empty results
90+
### Returning empty results
9191

9292
```golang
9393
mockedHTTPClient := NewMockedHTTPClient(
@@ -122,7 +122,7 @@ issues2, _, repo2Err := c.Issues.ListByRepo(ctx, "owner1", "repo2", &github.Issu
122122
// repo2Err == nil
123123
```
124124

125-
## Mocking errors from the API
125+
### Mocking errors from the API
126126

127127
```golang
128128
mockedHTTPClient := mock.NewMockedHTTPClient(
@@ -145,15 +145,15 @@ user, _, userErr := c.Users.Get(ctx, "someUser")
145145

146146
// user == nil
147147

148-
if userErr == nil {
148+
if userErr == nil {
149149
if ghErr, ok := userErr.(*github.ErrorResponse); ok {
150150
fmt.Println(ghErr.Message) // == "github went belly up or something"
151151
}
152152
}
153153

154154
```
155155

156-
## Mocking with pagination
156+
### Mocking with pagination
157157

158158
```golang
159159
mockedHTTPClient := NewMockedHTTPClient(
@@ -214,7 +214,7 @@ for {
214214
// len(allRepos) == 4
215215
```
216216

217-
## Mocking for Github Enterprise
217+
### Mocking for GitHub Enterprise
218218

219219
Github Enterprise uses a different prefix for its endpoints. In order to use the correct endpoints, please use the different set of `*Enterprise` options:
220220

@@ -240,16 +240,37 @@ user, _, userErr := c.Users.Get(ctx, "myuser")
240240
// user.Name == "foobar"
241241
```
242242

243-
# Why
243+
### Mocking with rate limits
244+
245+
`WithRateLimit` uses a single rate-limiting middleware across all endpoints on the mock router.
246+
247+
**NOTE:** This is an alpha feature. Future changes might break compatibility, until a stable version is released.
248+
249+
```go
250+
mhc := mock.NewMockedHTTPClient(
251+
mock.WithRequestMatchPages(
252+
mock.GetOrgsReposByOrg,
253+
[]github.Repository{{Name: github.String(repoOne)}},
254+
[]github.Repository{{Name: github.String(repoTwo)}},
255+
),
256+
257+
// The rate limiter will allow 10 requests per second, and a burst size of 1.
258+
// These two options together mean that the rate of requests will be strictly enforced, so if any two requests are
259+
// made less than 1/10th of a second apart, the latter will be refused and come back with a rate limit error.
260+
mock.WithRateLimit(10, 1),
261+
)
262+
```
263+
264+
## Why
244265

245-
Some conversations got started on [go-github#1800](https://github.com/google/go-github/issues/1800) since `go-github` didn't provide an interface that could be easily reimplemented for unittests. After lots of conversations from the folks from [go-github](https://github.com/google/go-github) and quite a few PR ideas later, this style of testing was deemed not suitable to be part of the core SDK as it's not a feature of the API itself. Nonetheless, the ability of writing unittests for code that uses the `go-github` package is critical.
266+
Some conversations got started on [go-github#1800](https://github.com/google/go-github/issues/1800) since `go-github` didn't provide an interface that could be easily reimplemented for unittests. After lots of conversations from the folks from [go-github](https://github.com/google/go-github) and quite a few PR ideas later, this style of testing was deemed not suitable to be part of the core SDK as it's not a feature of the API itself. Nonetheless, the ability of writing unittests for code that uses the `go-github` package is critical.
246267

247268
A reuseable, and not overly verbose, way of writing the tests was reached after some more interactions (months down the line) and here we are.
248269

249-
# Thanks
270+
## Thanks
250271

251272
Thanks for all ideas and feedback from the folks in [go-github](https://github.com/google/go-github/).
252273

253-
# License
274+
## License
254275

255276
This library is distributed under the MIT License found in LICENSE.

src/mock/server_options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func rateLimitMiddleware(rps float64, burst int) func(next http.Handler) http.Ha
159159
}
160160

161161
// WithRateLimit enforces a rate limit on the mocked [http.Client].
162+
// NOTE: This is an alpha feature. Future changes might break compatibility, until a stable version is released.
162163
func WithRateLimit(rps float64, burst int) MockBackendOption {
163164
return func(router *mux.Router) {
164165
router.Use(rateLimitMiddleware(rps, burst))

0 commit comments

Comments
 (0)