TestFDTable does a set of simple tests to make sure simple adds, removes, GetRefs, and DecRefs work. The ordering is just weird enough that a table-driven approach seemed clumsy.
(t *testing.T)
| 141 | // GetRefs, and DecRefs work. The ordering is just weird enough that a |
| 142 | // table-driven approach seemed clumsy. |
| 143 | func TestFDTable(t *testing.T) { |
| 144 | runTest(t, func(ctx context.Context, fdTable *FDTable, fd *vfs.FileDescription, limitSet *limits.LimitSet) { |
| 145 | // Cap the limit at one. |
| 146 | limitSet.Set(limits.NumberOfFiles, limits.Limit{1, maxFD}, true) |
| 147 | |
| 148 | if _, err := fdTable.NewFDs(ctx, 0, []*vfs.FileDescription{fd}, FDFlags{}); err != nil { |
| 149 | t.Fatalf("Adding an FD to an empty 1-size map: got %v, want nil", err) |
| 150 | } |
| 151 | |
| 152 | if _, err := fdTable.NewFDs(ctx, 0, []*vfs.FileDescription{fd}, FDFlags{}); err == nil { |
| 153 | t.Fatalf("Adding an FD to a filled 1-size map: got nil, wanted an error") |
| 154 | } |
| 155 | |
| 156 | // Remove the previous limit. |
| 157 | limitSet.Set(limits.NumberOfFiles, limits.Limit{maxFD, maxFD}, true) |
| 158 | |
| 159 | if fds, err := fdTable.NewFDs(ctx, 0, []*vfs.FileDescription{fd}, FDFlags{}); err != nil { |
| 160 | t.Fatalf("Adding an FD to a resized map: got %v, want nil", err) |
| 161 | } else if len(fds) != 1 || fds[0] != 1 { |
| 162 | t.Fatalf("Added an FD to a resized map: got %v, want {1}", fds) |
| 163 | } |
| 164 | |
| 165 | if df, err := fdTable.NewFDAt(ctx, 1, fd, FDFlags{}); err != nil { |
| 166 | t.Fatalf("Replacing FD 1 via fdTable.NewFDAt(1, r, FDFlags{}): got %v, wanted nil", err) |
| 167 | } else if df != nil { |
| 168 | t.Fatalf("fdTable.NewFDAt(1, r, FDFlags{}) displaced FD") |
| 169 | } |
| 170 | |
| 171 | if _, err := fdTable.NewFDAt(ctx, maxFD+1, fd, FDFlags{}); err == nil { |
| 172 | t.Fatalf("Using an FD that was too large via fdTable.NewFDAt(%v, r, FDFlags{}): got nil, wanted an error", maxFD+1) |
| 173 | } |
| 174 | |
| 175 | if ref, _ := fdTable.Get(1); ref == nil { |
| 176 | t.Fatalf("fdTable.Get(1): got nil, wanted %v", fd) |
| 177 | } |
| 178 | |
| 179 | if ref, _ := fdTable.Get(2); ref != nil { |
| 180 | t.Fatalf("fdTable.Get(2): got a %v, wanted nil", ref) |
| 181 | } |
| 182 | |
| 183 | ref := fdTable.Remove(ctx, 1) |
| 184 | if ref == nil { |
| 185 | t.Fatalf("fdTable.Remove(1) for an existing FD: failed, want success") |
| 186 | } |
| 187 | ref.DecRef(ctx) |
| 188 | |
| 189 | if ref := fdTable.Remove(ctx, 1); ref != nil { |
| 190 | t.Fatalf("r.Remove(1) for a removed FD: got success, want failure") |
| 191 | } |
| 192 | }) |
| 193 | } |
| 194 | |
| 195 | func TestDescriptorFlags(t *testing.T) { |
| 196 | runTest(t, func(ctx context.Context, fdTable *FDTable, fd *vfs.FileDescription, _ *limits.LimitSet) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…