|
| 1 | +package usecase |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/chaitin/MonkeyCode/backend/db" |
| 8 | + "github.com/chaitin/MonkeyCode/backend/pkg/taskflow" |
| 9 | +) |
| 10 | + |
| 11 | +func TestPickHostSelectsHostByRandomOffset(t *testing.T) { |
| 12 | + repo := &publicHostRepoStub{ |
| 13 | + hosts: []*db.Host{ |
| 14 | + {ID: "host-a", Hostname: "a", Weight: 1}, |
| 15 | + {ID: "host-b", Hostname: "b", Weight: 3}, |
| 16 | + {ID: "host-c", Hostname: "c", Weight: 1}, |
| 17 | + }, |
| 18 | + } |
| 19 | + hoster := &publicHosterStub{ |
| 20 | + onlineMap: map[string]bool{ |
| 21 | + "host-a": true, |
| 22 | + "host-b": true, |
| 23 | + "host-c": true, |
| 24 | + }, |
| 25 | + } |
| 26 | + u := &PublicHostUsecase{ |
| 27 | + repo: repo, |
| 28 | + taskflow: &taskflowClientStub{hoster: hoster}, |
| 29 | + } |
| 30 | + |
| 31 | + offsets := []uint64{0, 1, 3, 4} |
| 32 | + limits := make([]uint64, 0, len(offsets)) |
| 33 | + prevRandUint64n := randUint64n |
| 34 | + randUint64n = func(n uint64) (uint64, error) { |
| 35 | + limits = append(limits, n) |
| 36 | + v := offsets[0] |
| 37 | + offsets = offsets[1:] |
| 38 | + return v, nil |
| 39 | + } |
| 40 | + t.Cleanup(func() { |
| 41 | + randUint64n = prevRandUint64n |
| 42 | + }) |
| 43 | + |
| 44 | + got := make([]string, 0, 4) |
| 45 | + for range 4 { |
| 46 | + host, err := u.PickHost(context.Background()) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("PickHost() error = %v", err) |
| 49 | + } |
| 50 | + got = append(got, host.ID) |
| 51 | + } |
| 52 | + |
| 53 | + want := []string{"host-a", "host-b", "host-b", "host-c"} |
| 54 | + for i := range want { |
| 55 | + if got[i] != want[i] { |
| 56 | + t.Fatalf("PickHost() at %d = %q, want %q", i, got[i], want[i]) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + for _, limit := range limits { |
| 61 | + if limit != 5 { |
| 62 | + t.Fatalf("rand limit = %d, want 5", limit) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestPickHostTreatsNonPositiveWeightsAsOne(t *testing.T) { |
| 68 | + u := &PublicHostUsecase{ |
| 69 | + repo: &publicHostRepoStub{ |
| 70 | + hosts: []*db.Host{ |
| 71 | + {ID: "host-a", Hostname: "a", Weight: 0}, |
| 72 | + {ID: "host-b", Hostname: "b", Weight: -2}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + taskflow: &taskflowClientStub{ |
| 76 | + hoster: &publicHosterStub{ |
| 77 | + onlineMap: map[string]bool{ |
| 78 | + "host-a": true, |
| 79 | + "host-b": true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + prevRandUint64n := randUint64n |
| 86 | + randUint64n = func(n uint64) (uint64, error) { |
| 87 | + if n != 2 { |
| 88 | + t.Fatalf("rand limit = %d, want 2", n) |
| 89 | + } |
| 90 | + return 1, nil |
| 91 | + } |
| 92 | + t.Cleanup(func() { |
| 93 | + randUint64n = prevRandUint64n |
| 94 | + }) |
| 95 | + |
| 96 | + host, err := u.PickHost(context.Background()) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("PickHost() error = %v", err) |
| 99 | + } |
| 100 | + if host.ID != "host-b" { |
| 101 | + t.Fatalf("PickHost() = %q, want %q", host.ID, "host-b") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +type publicHostRepoStub struct { |
| 106 | + hosts []*db.Host |
| 107 | + err error |
| 108 | +} |
| 109 | + |
| 110 | +func (s *publicHostRepoStub) All(context.Context) ([]*db.Host, error) { |
| 111 | + return s.hosts, s.err |
| 112 | +} |
| 113 | + |
| 114 | +type publicHosterStub struct { |
| 115 | + onlineMap map[string]bool |
| 116 | + err error |
| 117 | +} |
| 118 | + |
| 119 | +func (s *publicHosterStub) List(context.Context, string) (map[string]*taskflow.Host, error) { |
| 120 | + return nil, nil |
| 121 | +} |
| 122 | + |
| 123 | +func (s *publicHosterStub) IsOnline(context.Context, *taskflow.IsOnlineReq[string]) (*taskflow.IsOnlineResp, error) { |
| 124 | + if s.err != nil { |
| 125 | + return nil, s.err |
| 126 | + } |
| 127 | + return &taskflow.IsOnlineResp{OnlineMap: s.onlineMap}, nil |
| 128 | +} |
| 129 | + |
| 130 | +type taskflowClientStub struct { |
| 131 | + hoster taskflow.Hoster |
| 132 | +} |
| 133 | + |
| 134 | +func (s *taskflowClientStub) VirtualMachiner() taskflow.VirtualMachiner { return nil } |
| 135 | +func (s *taskflowClientStub) Host() taskflow.Hoster { return s.hoster } |
| 136 | +func (s *taskflowClientStub) FileManager() taskflow.FileManager { return nil } |
| 137 | +func (s *taskflowClientStub) TaskManager() taskflow.TaskManager { return nil } |
| 138 | +func (s *taskflowClientStub) PortForwarder() taskflow.PortForwarder { return nil } |
| 139 | +func (s *taskflowClientStub) Stats(context.Context) (*taskflow.Stats, error) { |
| 140 | + return nil, nil |
| 141 | +} |
| 142 | +func (s *taskflowClientStub) TaskLive(context.Context, string, bool, func(*taskflow.TaskChunk) error) error { |
| 143 | + return nil |
| 144 | +} |
0 commit comments