grpc_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package scenarios
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/v2fly/v2ray-core/v5/common"
  8. "github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
  9. _ "github.com/v2fly/v2ray-core/v5/main/distro/all"
  10. )
  11. func TestGRPCDefault(t *testing.T) {
  12. tcpServer := tcp.Server{
  13. MsgProcessor: xor,
  14. }
  15. dest, err := tcpServer.Start()
  16. common.Must(err)
  17. defer tcpServer.Close()
  18. coreInst, InstMgrIfce := NewInstanceManagerCoreInstance()
  19. defer coreInst.Close()
  20. common.Must(InstMgrIfce.AddInstance(
  21. context.TODO(),
  22. "grpc_client",
  23. common.Must2(os.ReadFile("config/grpc_client.json")).([]byte),
  24. "jsonv5"))
  25. common.Must(InstMgrIfce.AddInstance(
  26. context.TODO(),
  27. "grpc_server",
  28. common.Must2(os.ReadFile("config/grpc_server.json")).([]byte),
  29. "jsonv5"))
  30. common.Must(InstMgrIfce.StartInstance(context.TODO(), "grpc_server"))
  31. common.Must(InstMgrIfce.StartInstance(context.TODO(), "grpc_client"))
  32. defer func() {
  33. common.Must(InstMgrIfce.StopInstance(context.TODO(), "grpc_server"))
  34. common.Must(InstMgrIfce.StopInstance(context.TODO(), "grpc_client"))
  35. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "grpc_server"))
  36. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "grpc_client"))
  37. coreInst.Close()
  38. }()
  39. if err := testTCPConnViaSocks(17784, dest.Port, 1024, time.Second*2)(); err != nil {
  40. t.Error(err)
  41. }
  42. }
  43. func TestGRPCWithServiceName(t *testing.T) {
  44. tcpServer := tcp.Server{
  45. MsgProcessor: xor,
  46. }
  47. dest, err := tcpServer.Start()
  48. common.Must(err)
  49. defer tcpServer.Close()
  50. coreInst, InstMgrIfce := NewInstanceManagerCoreInstance()
  51. defer coreInst.Close()
  52. common.Must(InstMgrIfce.AddInstance(
  53. context.TODO(),
  54. "grpc_client",
  55. common.Must2(os.ReadFile("config/grpc_servicename_client.json")).([]byte),
  56. "jsonv5"))
  57. common.Must(InstMgrIfce.AddInstance(
  58. context.TODO(),
  59. "grpc_server",
  60. common.Must2(os.ReadFile("config/grpc_servicename_server.json")).([]byte),
  61. "jsonv5"))
  62. common.Must(InstMgrIfce.StartInstance(context.TODO(), "grpc_server"))
  63. common.Must(InstMgrIfce.StartInstance(context.TODO(), "grpc_client"))
  64. defer func() {
  65. common.Must(InstMgrIfce.StopInstance(context.TODO(), "grpc_server"))
  66. common.Must(InstMgrIfce.StopInstance(context.TODO(), "grpc_client"))
  67. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "grpc_server"))
  68. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "grpc_client"))
  69. coreInst.Close()
  70. }()
  71. if err := testTCPConnViaSocks(17794, dest.Port, 1024, time.Second*2)(); err != nil {
  72. t.Error(err)
  73. }
  74. }