1
0

xray_lifecycle_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package service
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  6. )
  7. func TestXrayLifecycleSnapshotDoesNotOverwriteNewerResult(t *testing.T) {
  8. state := xrayLifecycle{}
  9. first := xray.NewProcess(&xray.Config{})
  10. second := xray.NewProcess(&xray.Config{})
  11. state.replace(first)
  12. state.storeResult(first, "first result")
  13. process, result := state.snapshot()
  14. if process != first || result != "first result" {
  15. t.Fatalf("snapshot = (%p, %q), want (%p, %q)", process, result, first, "first result")
  16. }
  17. state.replace(second)
  18. state.storeResult(first, "old result")
  19. process, result = state.snapshot()
  20. if process != second {
  21. t.Fatal("snapshot returned the replaced process")
  22. }
  23. if result != "" {
  24. t.Fatalf("snapshot result = %q, want empty", result)
  25. }
  26. }
  27. func TestXrayLifecycleConcurrentStatusResultAndTrafficReads(t *testing.T) {
  28. previousProcess, previousResult := xrayState.snapshot()
  29. t.Cleanup(func() {
  30. xrayState.mu.Lock()
  31. xrayState.process = previousProcess
  32. xrayState.result = previousResult
  33. xrayState.mu.Unlock()
  34. })
  35. first := xray.NewProcess(&xray.Config{})
  36. second := xray.NewProcess(&xray.Config{})
  37. service := XrayService{}
  38. var wg sync.WaitGroup
  39. wg.Go(func() {
  40. for range 200 {
  41. xrayState.replace(first)
  42. xrayState.replace(second)
  43. }
  44. })
  45. for range 4 {
  46. wg.Go(func() {
  47. for range 200 {
  48. _ = service.IsXrayRunning()
  49. _ = service.GetXrayResult()
  50. _, _, err := service.GetXrayTraffic()
  51. if err == nil || err.Error() != "xray is not running" {
  52. t.Errorf("GetXrayTraffic error = %v, want xray is not running", err)
  53. }
  54. }
  55. })
  56. }
  57. wg.Wait()
  58. }