This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
events_test.go
128 lines (100 loc) · 3.87 KB
/
events_test.go
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
// Copyright 2014 Tjerk Santegoeds
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oanda_test
import (
"sync"
"time"
"github.com/santegoeds/oanda"
"gopkg.in/check.v1"
)
type TestEventSuite struct {
OandaSuite
AccountId int
}
var _ = check.Suite(&TestEventSuite{})
func (ts *TestEventSuite) SetUpSuite(c *check.C) {
ts.OandaSuite.SetUpSuite(c)
ts.SetUpAccount(c)
}
func (ts *TestEventSuite) TearDownSuite(c *check.C) {
CancelAllOrders(c, ts.Client)
}
func (ts *TestEventSuite) TestEventApi(c *check.C) {
expiry := time.Now().Add(24 * time.Hour)
_, err := ts.Client.NewOrder(oanda.Limit, oanda.Buy, 1, "eur_usd", 0.75, expiry)
c.Assert(err, check.IsNil)
events, err := ts.Client.PollEvents(oanda.Count(1))
c.Assert(err, check.IsNil)
c.Log(events)
c.Assert(events, check.HasLen, 1)
c.Assert(events[0].AccountId(), check.Equals, ts.Client.AccountId())
c.Assert(events[0].Type(), check.Equals, "LIMIT_ORDER_CREATE")
orderCreate1, ok := events[0].(*oanda.OrderCreateEvent)
c.Assert(ok, check.Equals, true)
c.Assert(orderCreate1.Instrument(), check.Equals, "EUR_USD")
c.Assert(orderCreate1.Side(), check.Equals, "buy")
c.Assert(orderCreate1.Units(), check.Equals, 1)
c.Assert(orderCreate1.Price(), check.Equals, 0.75)
orderExpiry := orderCreate1.Expiry().Time()
c.Assert(orderExpiry.Equal(expiry.Truncate(time.Second)), check.Equals, true)
c.Assert(orderCreate1.Reason(), check.Equals, "CLIENT_REQUEST")
evt, err := ts.Client.PollEvent(orderCreate1.TranId())
c.Assert(err, check.IsNil)
c.Log(evt)
c.Check(evt.Type(), check.Equals, orderCreate1.Type())
c.Check(evt.AccountId(), check.Equals, orderCreate1.AccountId())
c.Check(evt.Time(), check.Equals, orderCreate1.Time())
orderCreate2, ok := evt.(*oanda.OrderCreateEvent)
c.Assert(ok, check.Equals, true)
c.Assert(orderCreate2.Instrument(), check.Equals, orderCreate1.Instrument())
c.Assert(orderCreate2.Side(), check.Equals, orderCreate1.Side())
c.Assert(orderCreate2.Units(), check.Equals, orderCreate1.Units())
c.Assert(orderCreate2.Price(), check.Equals, orderCreate1.Price())
c.Assert(orderCreate2.Expiry(), check.Equals, orderCreate1.Expiry())
c.Assert(orderCreate2.Reason(), check.Equals, orderCreate1.Reason())
}
func (ts *TestEventSuite) TestEventServer(c *check.C) {
es, err := ts.Client.NewEventServer(ts.Client.AccountId())
c.Assert(err, check.IsNil)
wg := sync.WaitGroup{}
t := time.AfterFunc(5*time.Minute, func() {
es.Stop()
c.Fail()
})
expiry := time.Now().Add(24 * time.Hour)
wg.Add(1)
go func() {
err := es.ConnectAndHandle(func(accountId oanda.Id, evt oanda.Event) {
c.Log(accountId, evt)
es.Stop()
t.Stop()
c.Assert(accountId, check.Equals, evt.AccountId())
c.Assert(evt.Type(), check.Equals, "LIMIT_ORDER_CREATE")
orderCreate, ok := evt.(*oanda.OrderCreateEvent)
c.Assert(ok, check.Equals, true)
c.Assert(orderCreate.Instrument(), check.Equals, "EUR_USD")
c.Assert(orderCreate.Side(), check.Equals, "buy")
c.Assert(orderCreate.Units(), check.Equals, 1)
c.Assert(orderCreate.Price(), check.Equals, 0.75)
orderExpiry := orderCreate.Expiry().Time()
c.Assert(orderExpiry.Equal(expiry.Truncate(time.Second)), check.Equals, true)
c.Assert(orderCreate.Reason(), check.Equals, "CLIENT_REQUEST")
})
c.Assert(err, check.IsNil)
wg.Done()
}()
time.Sleep(5 * time.Second)
ts.Client.NewOrder(oanda.Limit, oanda.Buy, 1, "eur_usd", 0.75, expiry)
wg.Wait()
}