| 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.DefaultServerConfig |
| 23 | clientConf := consts.DefaultClientConfig |
| 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 | [[proxies]] |
| 32 | name = "tcp" |
| 33 | type = "tcp" |
| 34 | localPort = %d |
| 35 | remotePort = %d |
| 36 | transport.bandwidthLimit = "10KB" |
| 37 | `, localPort, remotePort) |
| 38 |
| 39 | f.RunProcesses(serverConf, []string{clientConf}) |
| 40 |
| 41 | content := strings.Repeat("a", 50*1024) // 5KB |
| 42 | start := time.Now() |
| 43 | framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) { |
| 44 | r.Body([]byte(content)).Timeout(30 * time.Second) |
| 45 | }).ExpectResp([]byte(content)).Ensure() |
| 46 |
| 47 | duration := time.Since(start) |
| 48 | framework.Logf("request duration: %s", duration.String()) |
| 49 |
| 50 | framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String()) |
| 51 | }) |
| 52 |
| 53 | ginkgo.It("Proxy Bandwidth Limit by Server", func() { |
| 54 | // new test plugin server |
| 55 | newFunc := func() *plugin.Request { |
| 56 | var r plugin.Request |
| 57 | r.Content = &plugin.NewProxyContent{} |
| 58 | return &r |
| 59 | } |
| 60 | pluginPort := f.AllocPort() |
| 61 | handler := func(req *plugin.Request) *plugin.Response { |
| 62 | var ret plugin.Response |
| 63 | content := req.Content.(*plugin.NewProxyContent) |
| 64 | content.BandwidthLimit = "10KB" |
| 65 | content.BandwidthLimitMode = "server" |
| 66 | ret.Content = content |
| 67 | return &ret |
| 68 | } |
| 69 | pluginServer := pluginpkg.NewHTTPPluginServer(pluginPort, newFunc, handler, nil) |
| 70 |
| 71 | f.RunServer("", pluginServer) |
| 72 |
| 73 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 74 | [[httpPlugins]] |
| 75 | name = "test" |
| 76 | addr = "127.0.0.1:%d" |
| 77 | path = "/handler" |
| 78 | ops = ["NewProxy"] |
| 79 | `, pluginPort) |
| 80 | clientConf := consts.DefaultClientConfig |
| 81 |
| 82 | localPort := f.AllocPort() |
| 83 | localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort)) |
| 84 | f.RunServer("", localServer) |
| 85 |
| 86 | remotePort := f.AllocPort() |
| 87 | clientConf += fmt.Sprintf(` |
| 88 | [[proxies]] |
| 89 | name = "tcp" |
| 90 | type = "tcp" |
| 91 | localPort = %d |
| 92 | remotePort = %d |
| 93 | `, localPort, remotePort) |
| 94 |
| 95 | f.RunProcesses(serverConf, []string{clientConf}) |
| 96 |
| 97 | content := strings.Repeat("a", 50*1024) // 5KB |
| 98 | start := time.Now() |
| 99 | framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) { |
| 100 | r.Body([]byte(content)).Timeout(30 * time.Second) |
| 101 | }).ExpectResp([]byte(content)).Ensure() |
| 102 |
| 103 | duration := time.Since(start) |
| 104 | framework.Logf("request duration: %s", duration.String()) |
| 105 |
| 106 | framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String()) |
| 107 | }) |
| 108 | }) |