2021-06-20 23:57:41 +08:00
|
|
|
package features
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-02-27 14:44:16 +08:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2022-08-29 01:02:53 +08:00
|
|
|
|
2023-02-09 01:11:00 +08:00
|
|
|
plugin "github.com/fatedier/frp/pkg/plugin/server"
|
2021-06-20 23:57:41 +08:00
|
|
|
"github.com/fatedier/frp/test/e2e/framework"
|
|
|
|
"github.com/fatedier/frp/test/e2e/framework/consts"
|
|
|
|
"github.com/fatedier/frp/test/e2e/mock/server/streamserver"
|
2023-09-15 10:33:32 +08:00
|
|
|
pluginpkg "github.com/fatedier/frp/test/e2e/pkg/plugin"
|
2021-06-20 23:57:41 +08:00
|
|
|
"github.com/fatedier/frp/test/e2e/pkg/request"
|
|
|
|
)
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
var _ = ginkgo.Describe("[Feature: Bandwidth Limit]", func() {
|
2021-06-20 23:57:41 +08:00
|
|
|
f := framework.NewDefaultFramework()
|
|
|
|
|
2023-02-09 01:11:00 +08:00
|
|
|
ginkgo.It("Proxy Bandwidth Limit by Client", func() {
|
2021-06-20 23:57:41 +08:00
|
|
|
serverConf := consts.DefaultServerConfig
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
|
|
|
|
localPort := f.AllocPort()
|
|
|
|
localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort))
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
|
|
|
remotePort := f.AllocPort()
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "tcp"
|
|
|
|
type = "tcp"
|
|
|
|
localPort = %d
|
|
|
|
remotePort = %d
|
|
|
|
transport.bandwidthLimit = "10KB"
|
2021-06-20 23:57:41 +08:00
|
|
|
`, localPort, remotePort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
content := strings.Repeat("a", 50*1024) // 5KB
|
|
|
|
start := time.Now()
|
|
|
|
framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) {
|
|
|
|
r.Body([]byte(content)).Timeout(30 * time.Second)
|
|
|
|
}).ExpectResp([]byte(content)).Ensure()
|
2023-02-09 01:11:00 +08:00
|
|
|
|
|
|
|
duration := time.Since(start)
|
|
|
|
framework.Logf("request duration: %s", duration.String())
|
|
|
|
|
|
|
|
framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
ginkgo.It("Proxy Bandwidth Limit by Server", func() {
|
|
|
|
// new test plugin server
|
|
|
|
newFunc := func() *plugin.Request {
|
|
|
|
var r plugin.Request
|
|
|
|
r.Content = &plugin.NewProxyContent{}
|
|
|
|
return &r
|
|
|
|
}
|
|
|
|
pluginPort := f.AllocPort()
|
|
|
|
handler := func(req *plugin.Request) *plugin.Response {
|
|
|
|
var ret plugin.Response
|
|
|
|
content := req.Content.(*plugin.NewProxyContent)
|
|
|
|
content.BandwidthLimit = "10KB"
|
|
|
|
content.BandwidthLimitMode = "server"
|
|
|
|
ret.Content = content
|
|
|
|
return &ret
|
|
|
|
}
|
2023-09-15 10:33:32 +08:00
|
|
|
pluginServer := pluginpkg.NewHTTPPluginServer(pluginPort, newFunc, handler, nil)
|
2023-02-09 01:11:00 +08:00
|
|
|
|
|
|
|
f.RunServer("", pluginServer)
|
|
|
|
|
|
|
|
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[httpPlugins]]
|
|
|
|
name = "test"
|
|
|
|
addr = "127.0.0.1:%d"
|
|
|
|
path = "/handler"
|
|
|
|
ops = ["NewProxy"]
|
2023-02-09 01:11:00 +08:00
|
|
|
`, pluginPort)
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
|
|
|
|
localPort := f.AllocPort()
|
|
|
|
localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort))
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
|
|
|
remotePort := f.AllocPort()
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "tcp"
|
|
|
|
type = "tcp"
|
|
|
|
localPort = %d
|
|
|
|
remotePort = %d
|
2023-02-09 01:11:00 +08:00
|
|
|
`, localPort, remotePort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
content := strings.Repeat("a", 50*1024) // 5KB
|
|
|
|
start := time.Now()
|
|
|
|
framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) {
|
|
|
|
r.Body([]byte(content)).Timeout(30 * time.Second)
|
|
|
|
}).ExpectResp([]byte(content)).Ensure()
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
duration := time.Since(start)
|
2023-02-09 01:11:00 +08:00
|
|
|
framework.Logf("request duration: %s", duration.String())
|
2021-06-20 23:57:41 +08:00
|
|
|
|
2023-02-09 01:11:00 +08:00
|
|
|
framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String())
|
2021-06-20 23:57:41 +08:00
|
|
|
})
|
|
|
|
})
|