|
| 1 | +// Copyright 2025 Matrix Origin |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fileservice |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | +) |
| 21 | + |
| 22 | +func TestParseParallelModeVariants(t *testing.T) { |
| 23 | + cases := []struct { |
| 24 | + in string |
| 25 | + expect ParallelMode |
| 26 | + ok bool |
| 27 | + }{ |
| 28 | + {"", ParallelOff, true}, |
| 29 | + {"off", ParallelOff, true}, |
| 30 | + {"false", ParallelOff, true}, |
| 31 | + {"0", ParallelOff, true}, |
| 32 | + {"auto", ParallelAuto, true}, |
| 33 | + {"force", ParallelForce, true}, |
| 34 | + {"on", ParallelForce, true}, |
| 35 | + {"1", ParallelForce, true}, |
| 36 | + {"xxx", ParallelOff, false}, |
| 37 | + } |
| 38 | + |
| 39 | + for _, c := range cases { |
| 40 | + mode, ok := parseParallelMode(c.in) |
| 41 | + if mode != c.expect || ok != c.ok { |
| 42 | + t.Fatalf("input %s got (%v,%v) expect (%v,%v)", c.in, mode, ok, c.expect, c.ok) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestWithParallelModeOnContext(t *testing.T) { |
| 48 | + base := context.Background() |
| 49 | + ctx := WithParallelMode(base, ParallelForce) |
| 50 | + mode, ok := parallelModeFromContext(ctx) |
| 51 | + if !ok || mode != ParallelForce { |
| 52 | + t.Fatalf("expected force override, got %v %v", mode, ok) |
| 53 | + } |
| 54 | + if _, ok := parallelModeFromContext(base); ok { |
| 55 | + t.Fatalf("unexpected mode on base context") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestETLParallelModePriority(t *testing.T) { |
| 60 | + t.Setenv("MO_ETL_PARALLEL_MODE", "off") |
| 61 | + if m := etlParallelMode(context.Background()); m != ParallelOff { |
| 62 | + t.Fatalf("expect off from env, got %v", m) |
| 63 | + } |
| 64 | + |
| 65 | + ctx := WithParallelMode(context.Background(), ParallelForce) |
| 66 | + if m := etlParallelMode(ctx); m != ParallelForce { |
| 67 | + t.Fatalf("ctx override should win, got %v", m) |
| 68 | + } |
| 69 | + |
| 70 | + t.Setenv("MO_ETL_PARALLEL_MODE", "auto") |
| 71 | + if m := etlParallelMode(context.Background()); m != ParallelAuto { |
| 72 | + t.Fatalf("env auto not applied, got %v", m) |
| 73 | + } |
| 74 | +} |
0 commit comments