summaryrefslogtreecommitdiff
path: root/daemon/algod/api/server/lib/middlewares/connectionLimiter_test.go
blob: b3e5ea06a0c481427969b405d4a3dc12946514c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package middlewares_test

import (
	"errors"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/labstack/echo/v4"
	"github.com/stretchr/testify/assert"

	"github.com/algorand/go-algorand/daemon/algod/api/server/lib/middlewares"
	"github.com/algorand/go-algorand/test/partitiontest"
)

func TestConnectionLimiterBasic(t *testing.T) {
	partitiontest.PartitionTest(t)

	e := echo.New()

	handlerCh := make(chan struct{})
	limit := 5
	handler := func(c echo.Context) error {
		<-handlerCh
		return c.String(http.StatusOK, "test")
	}
	middleware := middlewares.MakeConnectionLimiter(uint64(limit))

	numConnections := 13
	for i := 0; i < 3; i++ {
		var recorders []*httptest.ResponseRecorder
		doneCh := make(chan int)
		errCh := make(chan error)

		for index := 0; index < numConnections; index++ {
			req := httptest.NewRequest(http.MethodGet, "/", nil)
			rec := httptest.NewRecorder()
			ctx := e.NewContext(req, rec)

			recorders = append(recorders, rec)

			go func(index int) {
				err := middleware(handler)(ctx)
				doneCh <- index
				errCh <- err
			}(index)
		}

		// Check http 429 code.
		for j := 0; j < numConnections-limit; j++ {
			index := <-doneCh
			assert.Equal(t, http.StatusTooManyRequests, recorders[index].Code)
		}

		// Let handlers finish.
		for j := 0; j < limit; j++ {
			handlerCh <- struct{}{}
		}

		// All other connections must return 200.
		for j := 0; j < limit; j++ {
			index := <-doneCh
			assert.Equal(t, http.StatusOK, recorders[index].Code)
		}

		// Check that no errors were returned by the middleware.
		for i := 0; i < numConnections; i++ {
			assert.NoError(t, <-errCh)
		}
	}
}

func TestConnectionLimiterForwardsError(t *testing.T) {
	partitiontest.PartitionTest(t)

	handlerError := errors.New("handler error")
	handler := func(c echo.Context) error {
		return handlerError
	}
	middleware := middlewares.MakeConnectionLimiter(1)

	err := middleware(handler)(nil)
	assert.ErrorIs(t, err, handlerError)
}