Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. |
| 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 android |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | ) |
| 20 | |
| 21 | func TestOncePer_Once(t *testing.T) { |
| 22 | once := OncePer{} |
| 23 | key := NewOnceKey("key") |
| 24 | |
| 25 | a := once.Once(key, func() interface{} { return "a" }).(string) |
| 26 | b := once.Once(key, func() interface{} { return "b" }).(string) |
| 27 | |
| 28 | if a != "a" { |
| 29 | t.Errorf(`first call to Once should return "a": %q`, a) |
| 30 | } |
| 31 | |
| 32 | if b != "a" { |
| 33 | t.Errorf(`second call to Once with the same key should return "a": %q`, b) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestOncePer_Get(t *testing.T) { |
| 38 | once := OncePer{} |
| 39 | key := NewOnceKey("key") |
| 40 | |
| 41 | a := once.Once(key, func() interface{} { return "a" }).(string) |
| 42 | b := once.Get(key).(string) |
| 43 | |
| 44 | if a != "a" { |
| 45 | t.Errorf(`first call to Once should return "a": %q`, a) |
| 46 | } |
| 47 | |
| 48 | if b != "a" { |
| 49 | t.Errorf(`Get with the same key should return "a": %q`, b) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestOncePer_Get_panic(t *testing.T) { |
| 54 | once := OncePer{} |
| 55 | key := NewOnceKey("key") |
| 56 | |
| 57 | defer func() { |
| 58 | p := recover() |
| 59 | |
| 60 | if p == nil { |
| 61 | t.Error("call to Get for unused key should panic") |
| 62 | } |
| 63 | }() |
| 64 | |
| 65 | once.Get(key) |
| 66 | } |
| 67 | |
| 68 | func TestOncePer_OnceStringSlice(t *testing.T) { |
| 69 | once := OncePer{} |
| 70 | key := NewOnceKey("key") |
| 71 | |
| 72 | a := once.OnceStringSlice(key, func() []string { return []string{"a"} }) |
| 73 | b := once.OnceStringSlice(key, func() []string { return []string{"a"} }) |
| 74 | |
| 75 | if a[0] != "a" { |
| 76 | t.Errorf(`first call to OnceStringSlice should return ["a"]: %q`, a) |
| 77 | } |
| 78 | |
| 79 | if b[0] != "a" { |
| 80 | t.Errorf(`second call to OnceStringSlice with the same key should return ["a"]: %q`, b) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestOncePer_Once2StringSlice(t *testing.T) { |
| 85 | once := OncePer{} |
| 86 | key := NewOnceKey("key") |
| 87 | |
| 88 | a, b := once.Once2StringSlice(key, func() ([]string, []string) { return []string{"a"}, []string{"b"} }) |
| 89 | c, d := once.Once2StringSlice(key, func() ([]string, []string) { return []string{"c"}, []string{"d"} }) |
| 90 | |
| 91 | if a[0] != "a" || b[0] != "b" { |
| 92 | t.Errorf(`first call to Once2StringSlice should return ["a"], ["b"]: %q, %q`, a, b) |
| 93 | } |
| 94 | |
| 95 | if c[0] != "a" || d[0] != "b" { |
| 96 | t.Errorf(`second call to Once2StringSlice with the same key should return ["a"], ["b"]: %q, %q`, c, d) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestNewOnceKey(t *testing.T) { |
| 101 | once := OncePer{} |
| 102 | key1 := NewOnceKey("key") |
| 103 | key2 := NewOnceKey("key") |
| 104 | |
| 105 | a := once.Once(key1, func() interface{} { return "a" }).(string) |
| 106 | b := once.Once(key2, func() interface{} { return "b" }).(string) |
| 107 | |
| 108 | if a != "a" { |
| 109 | t.Errorf(`first call to Once should return "a": %q`, a) |
| 110 | } |
| 111 | |
| 112 | if b != "b" { |
| 113 | t.Errorf(`second call to Once with the NewOnceKey from same string should return "b": %q`, b) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func TestNewCustomOnceKey(t *testing.T) { |
| 118 | type key struct { |
| 119 | key string |
| 120 | } |
| 121 | once := OncePer{} |
| 122 | key1 := NewCustomOnceKey(key{"key"}) |
| 123 | key2 := NewCustomOnceKey(key{"key"}) |
| 124 | |
| 125 | a := once.Once(key1, func() interface{} { return "a" }).(string) |
| 126 | b := once.Once(key2, func() interface{} { return "b" }).(string) |
| 127 | |
| 128 | if a != "a" { |
| 129 | t.Errorf(`first call to Once should return "a": %q`, a) |
| 130 | } |
| 131 | |
| 132 | if b != "a" { |
| 133 | t.Errorf(`second call to Once with the NewCustomOnceKey from equal key should return "a": %q`, b) |
| 134 | } |
| 135 | } |
Colin Cross | e5cdaf9 | 2019-02-11 15:06:16 -0800 | [diff] [blame^] | 136 | |
| 137 | func TestOncePerReentrant(t *testing.T) { |
| 138 | once := OncePer{} |
| 139 | key1 := NewOnceKey("key") |
| 140 | key2 := NewOnceKey("key") |
| 141 | |
| 142 | a := once.Once(key1, func() interface{} { return once.Once(key2, func() interface{} { return "a" }) }) |
| 143 | if a != "a" { |
| 144 | t.Errorf(`reentrant Once should return "a": %q`, a) |
| 145 | } |
| 146 | } |