Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.

Commit fd8b9df

Browse files
committed
Added tests
1 parent c997871 commit fd8b9df

39 files changed

+1104
-165
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.idea/
2+
cover.*
23
examples/5.single_binary_distribution/vendor.go
34
examples/test
45
examples/vendor
6+
testdata/tmp/*

astilectron.go

Lines changed: 88 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type Options struct {
6464
// New creates a new Astilectron instance
6565
func New(o Options) (a *Astilectron, err error) {
6666
// Validate the OS
67-
if err = validateOS(); err != nil {
67+
if err = validateOS(runtime.GOOS); err != nil {
6868
err = errors.Wrap(err, "validating OS failed")
6969
return
7070
}
@@ -79,7 +79,7 @@ func New(o Options) (a *Astilectron, err error) {
7979
}
8080

8181
// Set paths
82-
if a.paths, err = newPaths(o); err != nil {
82+
if a.paths, err = newPaths(runtime.GOOS, o); err != nil {
8383
err = errors.Wrap(err, "creating new paths failed")
8484
return
8585
}
@@ -105,9 +105,9 @@ func New(o Options) (a *Astilectron, err error) {
105105
}
106106

107107
// validateOS validates the OS
108-
func validateOS() error {
109-
if !astislice.InStringSlice(runtime.GOOS, validOSes) {
110-
return fmt.Errorf("OS %s is not supported", runtime.GOOS)
108+
func validateOS(os string) error {
109+
if !astislice.InStringSlice(os, validOSes) {
110+
return fmt.Errorf("OS %s is not supported", os)
111111
}
112112
return nil
113113
}
@@ -159,7 +159,7 @@ func (a *Astilectron) provision() error {
159159

160160
// Provision
161161
var ctx, _ = a.canceller.NewContext()
162-
return a.provisioner.Provision(ctx, a.options.AppName, *a.paths)
162+
return a.provisioner.Provision(ctx, a.options.AppName, runtime.GOOS, *a.paths)
163163
}
164164

165165
// listenTCP listens to the first TCP connection coming its way (this should be Astilectron)
@@ -172,58 +172,63 @@ func (a *Astilectron) listenTCP() (err error) {
172172
return errors.Wrap(err, "tcp net.Listen failed")
173173
}
174174

175-
// Accept
175+
// Check a connection has been accepted quickly enough
176176
var chanAccepted = make(chan bool)
177-
go func() {
178-
for i := 0; i <= 1; i++ {
179-
// Accept
180-
var conn net.Conn
181-
var err error
182-
if conn, err = a.listener.Accept(); err != nil {
183-
astilog.Errorf("%s while TCP accepting", err)
184-
a.dispatcher.dispatch(Event{Name: EventNameAppErrorAccept, TargetID: mainTargetID})
185-
a.dispatcher.dispatch(Event{Name: EventNameAppCmdStop, TargetID: mainTargetID})
186-
return
187-
}
188-
189-
// We only accept the first connection which should be Astilectron, close the next one and stop
190-
// the app
191-
if i > 0 {
192-
astilog.Errorf("Too many TCP connections")
193-
a.dispatcher.dispatch(Event{Name: EventNameAppTooManyAccept, TargetID: mainTargetID})
194-
a.dispatcher.dispatch(Event{Name: EventNameAppCmdStop, TargetID: mainTargetID})
195-
conn.Close()
196-
return
197-
}
198-
199-
// Let the timer know a connection has been accepted
200-
chanAccepted <- true
201-
202-
// Create reader and writer
203-
a.writer = newWriter(conn)
204-
a.reader = newReader(a.dispatcher, conn)
205-
go a.reader.read()
177+
go a.watchNoAccept(30*time.Second, chanAccepted)
178+
179+
// Accept connections
180+
go a.acceptTCP(chanAccepted)
181+
return
182+
}
183+
184+
// watchNoAccept checks whether a TCP connection is accepted quickly enough
185+
func (a *Astilectron) watchNoAccept(timeout time.Duration, chanAccepted chan bool) {
186+
var t = time.NewTimer(timeout)
187+
defer t.Stop()
188+
for {
189+
select {
190+
case <-chanAccepted:
191+
return
192+
case <-t.C:
193+
astilog.Errorf("No TCP connection has been accepted in the past %s", timeout)
194+
a.dispatcher.dispatch(Event{Name: EventNameAppNoAccept, TargetID: mainTargetID})
195+
a.dispatcher.dispatch(Event{Name: EventNameAppCmdStop, TargetID: mainTargetID})
196+
return
206197
}
207-
}()
198+
}
199+
}
208200

209-
// We check a connection has been accepted
210-
go func() {
211-
const timeout = 30 * time.Second
212-
var t = time.NewTimer(timeout)
213-
defer t.Stop()
214-
for {
215-
select {
216-
case <-chanAccepted:
217-
return
218-
case <-t.C:
219-
astilog.Errorf("No TCP connection has been accepted in the past %s", timeout)
220-
a.dispatcher.dispatch(Event{Name: EventNameAppNoAccept, TargetID: mainTargetID})
221-
a.dispatcher.dispatch(Event{Name: EventNameAppCmdStop, TargetID: mainTargetID})
222-
return
223-
}
201+
// watchAcceptTCP accepts TCP connections
202+
func (a *Astilectron) acceptTCP(chanAccepted chan bool) {
203+
for i := 0; i <= 1; i++ {
204+
// Accept
205+
var conn net.Conn
206+
var err error
207+
if conn, err = a.listener.Accept(); err != nil {
208+
astilog.Errorf("%s while TCP accepting", err)
209+
a.dispatcher.dispatch(Event{Name: EventNameAppErrorAccept, TargetID: mainTargetID})
210+
a.dispatcher.dispatch(Event{Name: EventNameAppCmdStop, TargetID: mainTargetID})
211+
return
224212
}
225-
}()
226-
return
213+
214+
// We only accept the first connection which should be Astilectron, close the next one and stop
215+
// the app
216+
if i > 0 {
217+
astilog.Errorf("Too many TCP connections")
218+
a.dispatcher.dispatch(Event{Name: EventNameAppTooManyAccept, TargetID: mainTargetID})
219+
a.dispatcher.dispatch(Event{Name: EventNameAppCmdStop, TargetID: mainTargetID})
220+
conn.Close()
221+
return
222+
}
223+
224+
// Let the timer know a connection has been accepted
225+
chanAccepted <- true
226+
227+
// Create reader and writer
228+
a.writer = newWriter(conn)
229+
a.reader = newReader(a.dispatcher, conn)
230+
go a.reader.read()
231+
}
227232
}
228233

229234
// execute executes Astilectron in Electron
@@ -239,7 +244,15 @@ func (a *Astilectron) execute() (err error) {
239244
cmd.Stderr = a.stderrWriter
240245
cmd.Stdout = a.stdoutWriter
241246

242-
// Start command
247+
// Execute command
248+
if err = a.executeCmd(cmd); err != nil {
249+
return errors.Wrap(err, "executing cmd failed")
250+
}
251+
return
252+
}
253+
254+
// executeCmd executes the command
255+
func (a *Astilectron) executeCmd(cmd *exec.Cmd) (err error) {
243256
var e = synchronousFunc(a.canceller, a, func() {
244257
// Start command
245258
astilog.Debugf("Starting cmd %s", strings.Join(cmd.Args, " "))
@@ -249,20 +262,7 @@ func (a *Astilectron) execute() (err error) {
249262
}
250263

251264
// Watch command
252-
go func() {
253-
// Wait
254-
cmd.Wait()
255-
256-
// Check the canceller to check whether it was a crash
257-
if !a.canceller.Cancelled() {
258-
astilog.Debug("App has crashed")
259-
a.dispatcher.dispatch(Event{Name: EventNameAppCrash, TargetID: mainTargetID})
260-
} else {
261-
astilog.Debug("App has closed")
262-
a.dispatcher.dispatch(Event{Name: EventNameAppClose, TargetID: mainTargetID})
263-
}
264-
a.dispatcher.dispatch(Event{Name: EventNameAppCmdStop, TargetID: mainTargetID})
265-
}()
265+
go a.watchCmd(cmd)
266266
}, EventNameAppEventReady)
267267

268268
// Update display pool
@@ -272,6 +272,22 @@ func (a *Astilectron) execute() (err error) {
272272
return
273273
}
274274

275+
// watchCmd watches the cmd execution
276+
func (a *Astilectron) watchCmd(cmd *exec.Cmd) {
277+
// Wait
278+
cmd.Wait()
279+
280+
// Check the canceller to check whether it was a crash
281+
if !a.canceller.Cancelled() {
282+
astilog.Debug("App has crashed")
283+
a.dispatcher.dispatch(Event{Name: EventNameAppCrash, TargetID: mainTargetID})
284+
} else {
285+
astilog.Debug("App has closed")
286+
a.dispatcher.dispatch(Event{Name: EventNameAppClose, TargetID: mainTargetID})
287+
}
288+
a.dispatcher.dispatch(Event{Name: EventNameAppCmdStop, TargetID: mainTargetID})
289+
}
290+
275291
// Displays returns the displays
276292
func (a *Astilectron) Displays() []*Display {
277293
return a.displayPool.all()
@@ -287,7 +303,9 @@ func (a *Astilectron) Close() {
287303
astilog.Debug("Closing...")
288304
a.canceller.Cancel()
289305
a.dispatcher.close()
290-
a.listener.Close()
306+
if a.listener != nil {
307+
a.listener.Close()
308+
}
291309
if a.reader != nil {
292310
a.reader.close()
293311
}

0 commit comments

Comments
 (0)