Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
fa84e50d
Commit
fa84e50d
authored
Oct 16, 2014
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
event: panic for duplicate type
parent
ade98091
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
1 deletion
+20
-1
event.go
event/event.go
+5
-1
event_test.go
event/event_test.go
+15
-0
No files found.
event/event.go
View file @
fa84e50d
...
@@ -3,6 +3,7 @@ package event
...
@@ -3,6 +3,7 @@ package event
import
(
import
(
"errors"
"errors"
"fmt"
"reflect"
"reflect"
"sync"
"sync"
)
)
...
@@ -40,6 +41,7 @@ var ErrMuxClosed = errors.New("event: mux closed")
...
@@ -40,6 +41,7 @@ var ErrMuxClosed = errors.New("event: mux closed")
func
(
mux
*
TypeMux
)
Subscribe
(
types
...
interface
{})
Subscription
{
func
(
mux
*
TypeMux
)
Subscribe
(
types
...
interface
{})
Subscription
{
sub
:=
newsub
(
mux
)
sub
:=
newsub
(
mux
)
mux
.
mutex
.
Lock
()
mux
.
mutex
.
Lock
()
defer
mux
.
mutex
.
Unlock
()
if
mux
.
stopped
{
if
mux
.
stopped
{
close
(
sub
.
postC
)
close
(
sub
.
postC
)
}
else
{
}
else
{
...
@@ -49,13 +51,15 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
...
@@ -49,13 +51,15 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
for
_
,
t
:=
range
types
{
for
_
,
t
:=
range
types
{
rtyp
:=
reflect
.
TypeOf
(
t
)
rtyp
:=
reflect
.
TypeOf
(
t
)
oldsubs
:=
mux
.
subm
[
rtyp
]
oldsubs
:=
mux
.
subm
[
rtyp
]
if
find
(
oldsubs
,
sub
)
!=
-
1
{
panic
(
fmt
.
Sprintf
(
"event: duplicate type %s in Subscribe"
,
rtyp
))
}
subs
:=
make
([]
*
muxsub
,
len
(
oldsubs
)
+
1
)
subs
:=
make
([]
*
muxsub
,
len
(
oldsubs
)
+
1
)
copy
(
subs
,
oldsubs
)
copy
(
subs
,
oldsubs
)
subs
[
len
(
oldsubs
)]
=
sub
subs
[
len
(
oldsubs
)]
=
sub
mux
.
subm
[
rtyp
]
=
subs
mux
.
subm
[
rtyp
]
=
subs
}
}
}
}
mux
.
mutex
.
Unlock
()
return
sub
return
sub
}
}
...
...
event/event_test.go
View file @
fa84e50d
...
@@ -60,6 +60,21 @@ func TestUnsubscribeUnblockPost(t *testing.T) {
...
@@ -60,6 +60,21 @@ func TestUnsubscribeUnblockPost(t *testing.T) {
}
}
}
}
func
TestSubscribeDuplicateType
(
t
*
testing
.
T
)
{
mux
:=
new
(
TypeMux
)
expected
:=
"event: duplicate type event.testEvent in Subscribe"
defer
func
()
{
err
:=
recover
()
if
err
==
nil
{
t
.
Errorf
(
"Subscribe didn't panic for duplicate type"
)
}
else
if
err
!=
expected
{
t
.
Errorf
(
"panic mismatch: got %#v, expected %#v"
,
err
,
expected
)
}
}()
mux
.
Subscribe
(
testEvent
(
1
),
testEvent
(
2
))
}
func
TestMuxConcurrent
(
t
*
testing
.
T
)
{
func
TestMuxConcurrent
(
t
*
testing
.
T
)
{
rand
.
Seed
(
time
.
Now
()
.
Unix
())
rand
.
Seed
(
time
.
Now
()
.
Unix
())
mux
:=
new
(
TypeMux
)
mux
:=
new
(
TypeMux
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment