| 9 |
| 10 | plugin "github.com/fatedier/frp/pkg/plugin/server" |
| 11 | "github.com/fatedier/frp/test/e2e/framework" |
| 12 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 13 | "github.com/fatedier/frp/test/e2e/mock/server/streamserver" |
| 14 | pluginpkg "github.com/fatedier/frp/test/e2e/pkg/plugin" |
| 15 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 16 | ) |
| 17 |
| 18 | var _ = ginkgo.Describe("[Feature: Bandwidth Limit]", func() { |
| 19 | f := framework.NewDefaultFramework() |
| 20 |
| 21 | ginkgo.It("Proxy Bandwidth Limit by Client", func() { |
| 22 | serverConf := consts.LegacyDefaultServerConfig |
| 23 | clientConf := consts.LegacyDefaultClientConfig |
| 24 |
| 25 | localPort := f.AllocPort() |
| 26 | localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort)) |
| 27 | f.RunServer("", localServer) |
| 28 |
| 29 | remotePort := f.AllocPort() |
| 30 | clientConf += fmt.Sprintf(` |
| 31 | [tcp] |
| 32 | type = tcp |
| 33 | local_port = %d |
| 34 | remote_port = %d |
| 35 | bandwidth_limit = 10KB |
| 36 | `, localPort, remotePort) |
| 37 |
| 38 | f.RunProcesses(serverConf, []string{clientConf}) |
| 39 |
| 40 | content := strings.Repeat("a", 50*1024) // 5KB |
| 41 | start := time.Now() |
| 42 | framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) { |
| 43 | r.Body([]byte(content)).Timeout(30 * time.Second) |
| 44 | }).ExpectResp([]byte(content)).Ensure() |
| 45 |
| 46 | duration := time.Since(start) |
| 47 | framework.Logf("request duration: %s", duration.String()) |
| 48 |
| 49 | framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String()) |
| 50 | }) |
| 51 |
| 52 | ginkgo.It("Proxy Bandwidth Limit by Server", func() { |
| 53 | // new test plugin server |
| 54 | newFunc := func() *plugin.Request { |
| 55 | var r plugin.Request |
| 56 | r.Content = &plugin.NewProxyContent{} |
| 57 | return &r |
| 58 | } |
| 59 | pluginPort := f.AllocPort() |
| 60 | handler := func(req *plugin.Request) *plugin.Response { |
| 61 | var ret plugin.Response |
| 62 | content := req.Content.(*plugin.NewProxyContent) |
| 63 | content.BandwidthLimit = "10KB" |
| 64 | content.BandwidthLimitMode = "server" |
| 65 | ret.Content = content |
| 66 | return &ret |
| 67 | } |
| 68 | pluginServer := pluginpkg.NewHTTPPluginServer(pluginPort, newFunc, handler, nil) |
| 69 |
| 70 | f.RunServer("", pluginServer) |
| 71 |
| 72 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 73 | [plugin.test] |
| 74 | addr = 127.0.0.1:%d |
| 75 | path = /handler |
| 76 | ops = NewProxy |
| 77 | `, pluginPort) |
| 78 | clientConf := consts.LegacyDefaultClientConfig |
| 79 |
| 80 | localPort := f.AllocPort() |
| 81 | localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort)) |
| 82 | f.RunServer("", localServer) |
| 83 |
| 84 | remotePort := f.AllocPort() |
| 85 | clientConf += fmt.Sprintf(` |
| 86 | [tcp] |
| 87 | type = tcp |
| 88 | local_port = %d |
| 89 | remote_port = %d |
| 90 | `, localPort, remotePort) |
| 91 |
| 92 | f.RunProcesses(serverConf, []string{clientConf}) |
| 93 |
| 94 | content := strings.Repeat("a", 50*1024) // 5KB |
| 95 | start := time.Now() |
| 96 | framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) { |
| 97 | r.Body([]byte(content)).Timeout(30 * time.Second) |
| 98 | }).ExpectResp([]byte(content)).Ensure() |
| 99 |
| 100 | duration := time.Since(start) |
| 101 | framework.Logf("request duration: %s", duration.String()) |
| 102 |
| 103 | framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String()) |
| 104 | }) |
| 105 | }) |