good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
89ba380b
Commit
89ba380b
authored
Jun 3, 2016
by
Felix Lange
Browse files
Options
Downloads
Plain Diff
Merge pull request #2656 from fjl/console-test
cmd/geth: make console tests more robust
parents
16a23ff7
b57b6e34
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
cmd/geth/consolecmd_test.go
+28
-18
28 additions, 18 deletions
cmd/geth/consolecmd_test.go
console/console.go
+8
-10
8 additions, 10 deletions
console/console.go
internal/jsre/jsre.go
+9
-7
9 additions, 7 deletions
internal/jsre/jsre.go
with
45 additions
and
35 deletions
cmd/geth/consolecmd_test.go
+
28
−
18
View file @
89ba380b
...
@@ -27,7 +27,6 @@ import (
...
@@ -27,7 +27,6 @@ import (
"testing"
"testing"
"time"
"time"
"github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/rpc"
)
)
...
@@ -37,9 +36,10 @@ func TestConsoleWelcome(t *testing.T) {
...
@@ -37,9 +36,10 @@ func TestConsoleWelcome(t *testing.T) {
coinbase
:=
"0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
coinbase
:=
"0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
// Start a geth console, make sure it's cleaned up and terminate the console
// Start a geth console, make sure it's cleaned up and terminate the console
geth
:=
runGeth
(
t
,
"--nat"
,
"none"
,
"--nodiscover"
,
"--etherbase"
,
coinbase
,
"-shh"
,
"console"
)
geth
:=
runGeth
(
t
,
defer
geth
.
expectExit
()
"--port"
,
"0"
,
"--maxpeers"
,
"0"
,
"--nodiscover"
,
"--nat"
,
"none"
,
geth
.
stdin
.
Close
()
"--etherbase"
,
coinbase
,
"--shh"
,
"console"
)
// Gather all the infos the welcome message needs to contain
// Gather all the infos the welcome message needs to contain
geth
.
setTemplateFunc
(
"goos"
,
func
()
string
{
return
runtime
.
GOOS
})
geth
.
setTemplateFunc
(
"goos"
,
func
()
string
{
return
runtime
.
GOOS
})
...
@@ -51,7 +51,6 @@ func TestConsoleWelcome(t *testing.T) {
...
@@ -51,7 +51,6 @@ func TestConsoleWelcome(t *testing.T) {
sort
.
Strings
(
apis
)
sort
.
Strings
(
apis
)
return
apis
return
apis
})
})
geth
.
setTemplateFunc
(
"prompt"
,
func
()
string
{
return
console
.
DefaultPrompt
})
// Verify the actual welcome message to the required template
// Verify the actual welcome message to the required template
geth
.
expect
(
`
geth
.
expect
(
`
...
@@ -63,52 +62,63 @@ at block: 0 ({{niltime}})
...
@@ -63,52 +62,63 @@ at block: 0 ({{niltime}})
datadir: {{.Datadir}}
datadir: {{.Datadir}}
modules:{{range apis}} {{.}}:1.0{{end}}
modules:{{range apis}} {{.}}:1.0{{end}}
{{prompt
}}
> {{.InputLine "exit"
}}
`
)
`
)
geth
.
expectExit
()
}
}
// Tests that a console can be attached to a running node via various means.
// Tests that a console can be attached to a running node via various means.
func
TestIPCAttachWelcome
(
t
*
testing
.
T
)
{
func
TestIPCAttachWelcome
(
t
*
testing
.
T
)
{
// Configure the instance for IPC attachement
// Configure the instance for IPC attachement
coinbase
:=
"0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
coinbase
:=
"0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
var
ipc
string
var
ipc
string
if
runtime
.
GOOS
==
"windows"
{
if
runtime
.
GOOS
==
"windows"
{
ipc
=
`\\.\pipe\geth`
+
strconv
.
Itoa
(
rand
.
Int
())
ipc
=
`\\.\pipe\geth`
+
strconv
.
Itoa
(
rand
.
Int
())
}
else
{
}
else
{
ws
:=
tmpdir
(
t
)
ws
:=
tmpdir
(
t
)
defer
os
.
RemoveAll
(
ws
)
defer
os
.
RemoveAll
(
ws
)
ipc
=
filepath
.
Join
(
ws
,
"geth.ipc"
)
ipc
=
filepath
.
Join
(
ws
,
"geth.ipc"
)
}
}
// Run the parent geth and attach with a child console
// Note: we need --shh because testAttachWelcome checks for default
geth
:=
runGeth
(
t
,
"--nat"
,
"none"
,
"--nodiscover"
,
"--etherbase"
,
coinbase
,
"-shh"
,
"--ipcpath"
,
ipc
)
// list of ipc modules and shh is included there.
defer
geth
.
interrupt
()
geth
:=
runGeth
(
t
,
"--port"
,
"0"
,
"--maxpeers"
,
"0"
,
"--nodiscover"
,
"--nat"
,
"none"
,
"--etherbase"
,
coinbase
,
"--shh"
,
"--ipcpath"
,
ipc
)
time
.
Sleep
(
2
*
time
.
Second
)
// Simple way to wait for the RPC endpoint to open
time
.
Sleep
(
2
*
time
.
Second
)
// Simple way to wait for the RPC endpoint to open
testAttachWelcome
(
t
,
geth
,
"ipc:"
+
ipc
)
testAttachWelcome
(
t
,
geth
,
"ipc:"
+
ipc
)
geth
.
interrupt
()
geth
.
expectExit
()
}
}
func
TestHTTPAttachWelcome
(
t
*
testing
.
T
)
{
func
TestHTTPAttachWelcome
(
t
*
testing
.
T
)
{
coinbase
:=
"0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
coinbase
:=
"0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
port
:=
strconv
.
Itoa
(
rand
.
Intn
(
65535
-
1024
)
+
1024
)
// Yeah, sometimes this will fail, sorry :P
port
:=
strconv
.
Itoa
(
rand
.
Intn
(
65535
-
1024
)
+
1024
)
// Yeah, sometimes this will fail, sorry :P
geth
:=
runGeth
(
t
,
geth
:=
runGeth
(
t
,
"--nat"
,
"none"
,
"--nodiscover"
,
"--etherbase"
,
coinbase
,
"--rpc"
,
"--rpcport"
,
port
)
"--port"
,
"0"
,
"--maxpeers"
,
"0"
,
"--nodiscover"
,
"--nat"
,
"none"
,
defer
geth
.
interrupt
(
)
"--etherbase"
,
coinbase
,
"--rpc"
,
"--rpcport"
,
port
)
time
.
Sleep
(
2
*
time
.
Second
)
// Simple way to wait for the RPC endpoint to open
time
.
Sleep
(
2
*
time
.
Second
)
// Simple way to wait for the RPC endpoint to open
testAttachWelcome
(
t
,
geth
,
"http://localhost:"
+
port
)
testAttachWelcome
(
t
,
geth
,
"http://localhost:"
+
port
)
geth
.
interrupt
()
geth
.
expectExit
()
}
}
func
TestWSAttachWelcome
(
t
*
testing
.
T
)
{
func
TestWSAttachWelcome
(
t
*
testing
.
T
)
{
coinbase
:=
"0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
coinbase
:=
"0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
port
:=
strconv
.
Itoa
(
rand
.
Intn
(
65535
-
1024
)
+
1024
)
// Yeah, sometimes this will fail, sorry :P
port
:=
strconv
.
Itoa
(
rand
.
Intn
(
65535
-
1024
)
+
1024
)
// Yeah, sometimes this will fail, sorry :P
geth
:=
runGeth
(
t
,
"--nat"
,
"none"
,
"--nodiscover"
,
"--etherbase"
,
coinbase
,
"--ws"
,
"--wsport"
,
port
)
geth
:=
runGeth
(
t
,
defer
geth
.
interrupt
()
"--port"
,
"0"
,
"--maxpeers"
,
"0"
,
"--nodiscover"
,
"--nat"
,
"none"
,
"--etherbase"
,
coinbase
,
"--ws"
,
"--wsport"
,
port
)
time
.
Sleep
(
2
*
time
.
Second
)
// Simple way to wait for the RPC endpoint to open
time
.
Sleep
(
2
*
time
.
Second
)
// Simple way to wait for the RPC endpoint to open
testAttachWelcome
(
t
,
geth
,
"ws://localhost:"
+
port
)
testAttachWelcome
(
t
,
geth
,
"ws://localhost:"
+
port
)
geth
.
interrupt
()
geth
.
expectExit
()
}
}
func
testAttachWelcome
(
t
*
testing
.
T
,
geth
*
testgeth
,
endpoint
string
)
{
func
testAttachWelcome
(
t
*
testing
.
T
,
geth
*
testgeth
,
endpoint
string
)
{
...
@@ -135,7 +145,6 @@ func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
...
@@ -135,7 +145,6 @@ func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
sort
.
Strings
(
apis
)
sort
.
Strings
(
apis
)
return
apis
return
apis
})
})
attach
.
setTemplateFunc
(
"prompt"
,
func
()
string
{
return
console
.
DefaultPrompt
})
// Verify the actual welcome message to the required template
// Verify the actual welcome message to the required template
attach
.
expect
(
`
attach
.
expect
(
`
...
@@ -147,6 +156,7 @@ at block: 0 ({{niltime}}){{if ipc}}
...
@@ -147,6 +156,7 @@ at block: 0 ({{niltime}}){{if ipc}}
datadir: {{datadir}}{{end}}
datadir: {{datadir}}{{end}}
modules:{{range apis}} {{.}}:1.0{{end}}
modules:{{range apis}} {{.}}:1.0{{end}}
{{prompt
}}
> {{.InputLine "exit"
}}
`
)
`
)
attach
.
expectExit
()
}
}
This diff is collapsed.
Click to expand it.
console/console.go
+
8
−
10
View file @
89ba380b
...
@@ -244,14 +244,12 @@ func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, str
...
@@ -244,14 +244,12 @@ func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, str
// console's available modules.
// console's available modules.
func
(
c
*
Console
)
Welcome
()
{
func
(
c
*
Console
)
Welcome
()
{
// Print some generic Geth metadata
// Print some generic Geth metadata
fmt
.
Fprintf
(
c
.
printer
,
"Welcome to the Geth JavaScript console!
\n\n
"
)
c
.
jsre
.
Run
(
`
c
.
jsre
.
Run
(
`
(function () {
console.log("Welcome to the Geth JavaScript console!\n");
console.log("instance: " + web3.version.node);
console.log("instance: " + web3.version.node);
console.log("coinbase: " + eth.coinbase);
console.log("coinbase: " + eth.coinbase);
console.log("at block: " + eth.blockNumber + " (" + new Date(1000 * eth.getBlock(eth.blockNumber).timestamp) + ")");
console.log("at block: " + eth.blockNumber + " (" + new Date(1000 * eth.getBlock(eth.blockNumber).timestamp) + ")");
console.log(" datadir: " + admin.datadir);
console.log(" datadir: " + admin.datadir);
})();
`
)
`
)
// List all the supported modules for the user to call
// List all the supported modules for the user to call
if
apis
,
err
:=
c
.
client
.
SupportedModules
();
err
==
nil
{
if
apis
,
err
:=
c
.
client
.
SupportedModules
();
err
==
nil
{
...
@@ -260,9 +258,9 @@ func (c *Console) Welcome() {
...
@@ -260,9 +258,9 @@ func (c *Console) Welcome() {
modules
=
append
(
modules
,
fmt
.
Sprintf
(
"%s:%s"
,
api
,
version
))
modules
=
append
(
modules
,
fmt
.
Sprintf
(
"%s:%s"
,
api
,
version
))
}
}
sort
.
Strings
(
modules
)
sort
.
Strings
(
modules
)
c
.
jsre
.
Run
(
"(function () { console.log('
modules:
"
+
strings
.
Join
(
modules
,
" "
)
+
"'); })();"
)
fmt
.
Fprintln
(
c
.
printer
,
"
modules:
"
,
strings
.
Join
(
modules
,
" "
))
}
}
c
.
jsre
.
Run
(
"(function () { console.log(); })();"
)
fmt
.
Fprintln
(
c
.
printer
)
}
}
// Evaluate executes code and pretty prints the result to the specified output
// Evaluate executes code and pretty prints the result to the specified output
...
...
This diff is collapsed.
Click to expand it.
internal/jsre/jsre.go
+
9
−
7
View file @
89ba380b
...
@@ -24,7 +24,6 @@ import (
...
@@ -24,7 +24,6 @@ import (
"io"
"io"
"io/ioutil"
"io/ioutil"
"math/rand"
"math/rand"
"sync"
"time"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
...
@@ -44,7 +43,7 @@ type JSRE struct {
...
@@ -44,7 +43,7 @@ type JSRE struct {
output
io
.
Writer
output
io
.
Writer
evalQueue
chan
*
evalReq
evalQueue
chan
*
evalReq
stopEventLoop
chan
bool
stopEventLoop
chan
bool
lo
opWg
sync
.
WaitGroup
c
lo
sed
chan
struct
{}
}
}
// jsTimer is a single timer instance with a callback function
// jsTimer is a single timer instance with a callback function
...
@@ -66,10 +65,10 @@ func New(assetPath string, output io.Writer) *JSRE {
...
@@ -66,10 +65,10 @@ func New(assetPath string, output io.Writer) *JSRE {
re
:=
&
JSRE
{
re
:=
&
JSRE
{
assetPath
:
assetPath
,
assetPath
:
assetPath
,
output
:
output
,
output
:
output
,
closed
:
make
(
chan
struct
{}),
evalQueue
:
make
(
chan
*
evalReq
),
evalQueue
:
make
(
chan
*
evalReq
),
stopEventLoop
:
make
(
chan
bool
),
stopEventLoop
:
make
(
chan
bool
),
}
}
re
.
loopWg
.
Add
(
1
)
go
re
.
runEventLoop
()
go
re
.
runEventLoop
()
re
.
Set
(
"loadScript"
,
re
.
loadScript
)
re
.
Set
(
"loadScript"
,
re
.
loadScript
)
re
.
Set
(
"inspect"
,
prettyPrintJS
)
re
.
Set
(
"inspect"
,
prettyPrintJS
)
...
@@ -98,6 +97,8 @@ func randomSource() *rand.Rand {
...
@@ -98,6 +97,8 @@ func randomSource() *rand.Rand {
// functions should be used if and only if running a routine that was already
// functions should be used if and only if running a routine that was already
// called from JS through an RPC call.
// called from JS through an RPC call.
func
(
self
*
JSRE
)
runEventLoop
()
{
func
(
self
*
JSRE
)
runEventLoop
()
{
defer
close
(
self
.
closed
)
vm
:=
otto
.
New
()
vm
:=
otto
.
New
()
r
:=
randomSource
()
r
:=
randomSource
()
vm
.
SetRandomSource
(
r
.
Float64
)
vm
.
SetRandomSource
(
r
.
Float64
)
...
@@ -213,8 +214,6 @@ loop:
...
@@ -213,8 +214,6 @@ loop:
timer
.
timer
.
Stop
()
timer
.
timer
.
Stop
()
delete
(
registry
,
timer
)
delete
(
registry
,
timer
)
}
}
self
.
loopWg
.
Done
()
}
}
// Do executes the given function on the JS event loop.
// Do executes the given function on the JS event loop.
...
@@ -227,8 +226,11 @@ func (self *JSRE) Do(fn func(*otto.Otto)) {
...
@@ -227,8 +226,11 @@ func (self *JSRE) Do(fn func(*otto.Otto)) {
// stops the event loop before exit, optionally waits for all timers to expire
// stops the event loop before exit, optionally waits for all timers to expire
func
(
self
*
JSRE
)
Stop
(
waitForCallbacks
bool
)
{
func
(
self
*
JSRE
)
Stop
(
waitForCallbacks
bool
)
{
self
.
stopEventLoop
<-
waitForCallbacks
select
{
self
.
loopWg
.
Wait
()
case
<-
self
.
closed
:
case
self
.
stopEventLoop
<-
waitForCallbacks
:
<-
self
.
closed
}
}
}
// Exec(file) loads and runs the contents of a file
// Exec(file) loads and runs the contents of a file
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment