-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_loader_test.go
More file actions
133 lines (108 loc) · 2.97 KB
/
class_loader_test.go
File metadata and controls
133 lines (108 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// GNU GPL v3 License
// Copyright (c) 2017 github.com/ymhhh
package classloader
import (
"errors"
"reflect"
"sync"
"testing"
)
type sampleClass struct{}
func (p *sampleClass) Construct() {}
type sampleClassWithArgs struct {
Value int
}
func (p *sampleClassWithArgs) Construct(value int) {
p.Value = value
}
func (p *sampleClassWithArgs) ConstructWithError() error {
return nil
}
func TestLoadClassAndFindByName(t *testing.T) {
cl := NewClassLoader(nil)
if err := cl.LoadClass("test:Sample", (*sampleClass)(nil)); err != nil {
t.Fatalf("LoadClass: %v", err)
}
typ, ok := cl.FindClass("test:Sample")
if !ok {
t.Fatal("FindClass by name: not found")
}
if typ != reflect.TypeOf(sampleClass{}) {
t.Fatalf("FindClass by name: got %v", typ)
}
}
func TestLoadClassAndFindByType(t *testing.T) {
cl := NewClassLoader(nil)
if err := cl.LoadClass("", (*sampleClass)(nil)); err != nil {
t.Fatalf("LoadClass: %v", err)
}
typ, ok := cl.FindClass((*sampleClass)(nil))
if !ok {
t.Fatal("FindClass by type: not found")
}
if typ != reflect.TypeOf(sampleClass{}) {
t.Fatalf("FindClass by type: got %v", typ)
}
}
func TestLoadClassParentDelegation(t *testing.T) {
parent := NewClassLoader(nil)
child := NewClassLoader(parent)
if err := parent.LoadClass("parent:Sample", (*sampleClass)(nil)); err != nil {
t.Fatalf("parent LoadClass: %v", err)
}
typ, ok := child.FindClass("parent:Sample")
if !ok {
t.Fatal("child should delegate to parent")
}
if typ != reflect.TypeOf(sampleClass{}) {
t.Fatalf("unexpected type: %v", typ)
}
if err := child.LoadClass("parent:Sample", (*sampleClass)(nil)); err != nil {
t.Fatalf("child LoadClass when parent has class: %v", err)
}
if _, ok := child.(*defaultClassLoader).nameTypes["parent:Sample"]; ok {
t.Fatal("child should not duplicate class registered in parent")
}
}
func TestLoadClassInvalidInput(t *testing.T) {
cl := NewClassLoader(nil)
if err := cl.LoadClass("x", nil); !errors.Is(err, ErrNilClassValue) {
t.Fatalf("nil value: got %v", err)
}
if err := cl.LoadClass("x", "name"); !errors.Is(err, ErrInvalidClassType) {
t.Fatalf("invalid type: got %v", err)
}
}
func TestLoadClassConcurrent(t *testing.T) {
cl := NewClassLoader(nil)
var wg sync.WaitGroup
for i := 0; i < 200; i++ {
wg.Add(2)
go func() {
defer wg.Done()
if err := cl.LoadClass("concurrent:Sample", (*sampleClass)(nil)); err != nil {
t.Errorf("LoadClass: %v", err)
}
}()
go func() {
defer wg.Done()
cl.FindClass("concurrent:Sample")
}()
}
wg.Wait()
if !cl.FindLoadedClass("concurrent:Sample") {
t.Fatal("class should be loaded after concurrent access")
}
}
func TestFindLoadedClass(t *testing.T) {
cl := NewClassLoader(nil)
if cl.FindLoadedClass("missing") {
t.Fatal("FindLoadedClass should be false before load")
}
if err := cl.LoadClass("loaded", (*sampleClass)(nil)); err != nil {
t.Fatalf("LoadClass: %v", err)
}
if !cl.FindLoadedClass("loaded") {
t.Fatal("FindLoadedClass should be true after load")
}
}