matcher_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Copyright (c) 2023, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package inproxy
  20. import (
  21. "context"
  22. "fmt"
  23. "strings"
  24. "sync"
  25. "testing"
  26. "time"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  30. )
  31. func TestMatcher(t *testing.T) {
  32. err := runTestMatcher()
  33. if err != nil {
  34. t.Errorf(errors.Trace(err).Error())
  35. }
  36. }
  37. func runTestMatcher() error {
  38. limitEntryCount := 50
  39. rateLimitQuantity := 100
  40. rateLimitInterval := 1000 * time.Millisecond
  41. logger := newTestLogger()
  42. m := NewMatcher(
  43. &MatcherConfig{
  44. Logger: logger,
  45. AnnouncementLimitEntryCount: limitEntryCount,
  46. AnnouncementRateLimitQuantity: rateLimitQuantity,
  47. AnnouncementRateLimitInterval: rateLimitInterval,
  48. OfferLimitEntryCount: limitEntryCount,
  49. OfferRateLimitQuantity: rateLimitQuantity,
  50. OfferRateLimitInterval: rateLimitInterval,
  51. })
  52. err := m.Start()
  53. if err != nil {
  54. return errors.Trace(err)
  55. }
  56. defer m.Stop()
  57. makeID := func() ID {
  58. ID, err := MakeID()
  59. if err != nil {
  60. panic(err)
  61. }
  62. return ID
  63. }
  64. makeAnnouncement := func(properties *MatchProperties) *MatchAnnouncement {
  65. return &MatchAnnouncement{
  66. Properties: *properties,
  67. ProxyID: makeID(),
  68. ConnectionID: makeID(),
  69. }
  70. }
  71. makeOffer := func(properties *MatchProperties) *MatchOffer {
  72. return &MatchOffer{
  73. Properties: *properties,
  74. ClientProxyProtocolVersion: ProxyProtocolVersion1,
  75. }
  76. }
  77. checkMatchMetrics := func(metrics *MatchMetrics) error {
  78. if metrics.OfferQueueSize < 1 || metrics.AnnouncementQueueSize < 1 {
  79. return errors.TraceNew("unexpected match metrics")
  80. }
  81. return nil
  82. }
  83. proxyIP := randomIPAddress()
  84. proxyFunc := func(
  85. resultChan chan error,
  86. proxyIP string,
  87. matchProperties *MatchProperties,
  88. timeout time.Duration,
  89. waitBeforeAnswer chan struct{},
  90. answerSuccess bool) {
  91. ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
  92. defer cancelFunc()
  93. announcement := makeAnnouncement(matchProperties)
  94. offer, matchMetrics, err := m.Announce(ctx, proxyIP, announcement)
  95. if err != nil {
  96. resultChan <- errors.Trace(err)
  97. return
  98. } else {
  99. err := checkMatchMetrics(matchMetrics)
  100. if err != nil {
  101. resultChan <- errors.Trace(err)
  102. return
  103. }
  104. }
  105. if waitBeforeAnswer != nil {
  106. <-waitBeforeAnswer
  107. }
  108. if answerSuccess {
  109. err = m.Answer(
  110. &MatchAnswer{
  111. ProxyID: announcement.ProxyID,
  112. ConnectionID: announcement.ConnectionID,
  113. SelectedProxyProtocolVersion: offer.ClientProxyProtocolVersion,
  114. })
  115. } else {
  116. m.AnswerError(announcement.ProxyID, announcement.ConnectionID)
  117. }
  118. resultChan <- errors.Trace(err)
  119. }
  120. clientIP := randomIPAddress()
  121. clientFunc := func(
  122. resultChan chan error,
  123. clientIP string,
  124. matchProperties *MatchProperties,
  125. timeout time.Duration) {
  126. ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
  127. defer cancelFunc()
  128. offer := makeOffer(matchProperties)
  129. answer, _, matchMetrics, err := m.Offer(ctx, clientIP, offer)
  130. if err != nil {
  131. resultChan <- errors.Trace(err)
  132. return
  133. }
  134. if answer.SelectedProxyProtocolVersion != offer.ClientProxyProtocolVersion {
  135. resultChan <- errors.TraceNew("unexpected selected proxy protocol version")
  136. return
  137. } else {
  138. err := checkMatchMetrics(matchMetrics)
  139. if err != nil {
  140. resultChan <- errors.Trace(err)
  141. return
  142. }
  143. }
  144. resultChan <- nil
  145. }
  146. // Test: announce timeout
  147. proxyResultChan := make(chan error)
  148. go proxyFunc(proxyResultChan, proxyIP, &MatchProperties{}, 1*time.Microsecond, nil, true)
  149. err = <-proxyResultChan
  150. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  151. return errors.Tracef("unexpected result: %v", err)
  152. }
  153. if m.announcementQueue.Len() != 0 {
  154. return errors.TraceNew("unexpected queue size")
  155. }
  156. // Test: limit announce entries by IP
  157. time.Sleep(rateLimitInterval)
  158. maxEntries := limitEntryCount
  159. maxEntriesProxyResultChan := make(chan error, maxEntries)
  160. // fill the queue with max entries for one IP; the first one will timeout sooner
  161. go proxyFunc(maxEntriesProxyResultChan, proxyIP, &MatchProperties{}, 10*time.Millisecond, nil, true)
  162. for i := 0; i < maxEntries-1; i++ {
  163. go proxyFunc(maxEntriesProxyResultChan, proxyIP, &MatchProperties{}, 100*time.Millisecond, nil, true)
  164. }
  165. // await goroutines filling queue
  166. for {
  167. time.Sleep(10 * time.Microsecond)
  168. m.announcementQueueMutex.Lock()
  169. queueLen := m.announcementQueue.Len()
  170. m.announcementQueueMutex.Unlock()
  171. if queueLen == maxEntries {
  172. break
  173. }
  174. }
  175. // the next enqueue should fail with "max entries"
  176. go proxyFunc(proxyResultChan, proxyIP, &MatchProperties{}, 10*time.Millisecond, nil, true)
  177. err = <-proxyResultChan
  178. if err == nil || !strings.HasSuffix(err.Error(), "max entries for IP") {
  179. return errors.Tracef("unexpected result: %v", err)
  180. }
  181. // wait for first entry to timeout
  182. err = <-maxEntriesProxyResultChan
  183. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  184. return errors.Tracef("unexpected result: %v", err)
  185. }
  186. // now another enqueue succeeds as expected
  187. go proxyFunc(proxyResultChan, proxyIP, &MatchProperties{}, 10*time.Millisecond, nil, true)
  188. err = <-proxyResultChan
  189. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  190. return errors.Tracef("unexpected result: %v", err)
  191. }
  192. // drain remaining entries
  193. for i := 0; i < maxEntries-1; i++ {
  194. err = <-maxEntriesProxyResultChan
  195. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  196. return errors.Tracef("unexpected result: %v", err)
  197. }
  198. }
  199. // Test: offer timeout
  200. clientResultChan := make(chan error)
  201. go clientFunc(clientResultChan, clientIP, &MatchProperties{}, 1*time.Microsecond)
  202. err = <-clientResultChan
  203. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  204. return errors.Tracef("unexpected result: %v", err)
  205. }
  206. if m.offerQueue.Len() != 0 {
  207. return errors.TraceNew("unexpected queue size")
  208. }
  209. // Test: limit offer entries by IP
  210. time.Sleep(rateLimitInterval)
  211. maxEntries = limitEntryCount
  212. maxEntriesClientResultChan := make(chan error, maxEntries)
  213. // fill the queue with max entries for one IP; the first one will timeout sooner
  214. go clientFunc(maxEntriesClientResultChan, clientIP, &MatchProperties{}, 10*time.Millisecond)
  215. for i := 0; i < maxEntries-1; i++ {
  216. go clientFunc(maxEntriesClientResultChan, clientIP, &MatchProperties{}, 100*time.Millisecond)
  217. }
  218. // await goroutines filling queue
  219. for {
  220. time.Sleep(10 * time.Microsecond)
  221. m.offerQueueMutex.Lock()
  222. queueLen := m.offerQueue.Len()
  223. m.offerQueueMutex.Unlock()
  224. if queueLen == maxEntries {
  225. break
  226. }
  227. }
  228. // enqueue should fail with "max entries"
  229. go clientFunc(clientResultChan, clientIP, &MatchProperties{}, 10*time.Millisecond)
  230. err = <-clientResultChan
  231. if err == nil || !strings.HasSuffix(err.Error(), "max entries for IP") {
  232. return errors.Tracef("unexpected result: %v", err)
  233. }
  234. // wait for first entry to timeout
  235. err = <-maxEntriesClientResultChan
  236. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  237. return errors.Tracef("unexpected result: %v", err)
  238. }
  239. // now another enqueue succeeds as expected
  240. go clientFunc(clientResultChan, clientIP, &MatchProperties{}, 10*time.Millisecond)
  241. err = <-clientResultChan
  242. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  243. return errors.Tracef("unexpected result: %v", err)
  244. }
  245. // drain remaining entries
  246. for i := 0; i < maxEntries-1; i++ {
  247. err = <-maxEntriesClientResultChan
  248. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  249. return errors.Tracef("unexpected result: %v", err)
  250. }
  251. }
  252. // Test: announcement rate limit
  253. m.SetLimits(
  254. 0, rateLimitQuantity, rateLimitInterval, []ID{},
  255. 0, rateLimitQuantity, rateLimitInterval)
  256. time.Sleep(rateLimitInterval)
  257. maxEntries = rateLimitQuantity
  258. maxEntriesProxyResultChan = make(chan error, maxEntries)
  259. waitGroup := new(sync.WaitGroup)
  260. for i := 0; i < maxEntries; i++ {
  261. waitGroup.Add(1)
  262. go func() {
  263. defer waitGroup.Done()
  264. proxyFunc(maxEntriesProxyResultChan, proxyIP, &MatchProperties{}, 1*time.Microsecond, nil, true)
  265. }()
  266. }
  267. // Use a wait group to ensure all maxEntries have hit the rate limiter
  268. // without sleeping before the next attempt, as any sleep can increase
  269. // the rate limiter token count.
  270. waitGroup.Wait()
  271. // the next enqueue should fail with "rate exceeded"
  272. go proxyFunc(proxyResultChan, proxyIP, &MatchProperties{}, 10*time.Millisecond, nil, true)
  273. err = <-proxyResultChan
  274. if err == nil || !strings.HasSuffix(err.Error(), "rate exceeded for IP") {
  275. return errors.Tracef("unexpected result: %v", err)
  276. }
  277. // Test: offer rate limit
  278. maxEntries = rateLimitQuantity
  279. maxEntriesClientResultChan = make(chan error, maxEntries)
  280. waitGroup = new(sync.WaitGroup)
  281. for i := 0; i < rateLimitQuantity; i++ {
  282. waitGroup.Add(1)
  283. go func() {
  284. defer waitGroup.Done()
  285. clientFunc(maxEntriesClientResultChan, clientIP, &MatchProperties{}, 1*time.Microsecond)
  286. }()
  287. }
  288. waitGroup.Wait()
  289. // enqueue should fail with "rate exceeded"
  290. go clientFunc(clientResultChan, clientIP, &MatchProperties{}, 10*time.Millisecond)
  291. err = <-clientResultChan
  292. if err == nil || !strings.HasSuffix(err.Error(), "rate exceeded for IP") {
  293. return errors.Tracef("unexpected result: %v", err)
  294. }
  295. time.Sleep(rateLimitInterval)
  296. m.SetLimits(
  297. limitEntryCount, rateLimitQuantity, rateLimitInterval, []ID{},
  298. limitEntryCount, rateLimitQuantity, rateLimitInterval)
  299. // Test: basic match
  300. basicCommonCompartmentIDs := []ID{makeID()}
  301. geoIPData1 := &MatchProperties{
  302. GeoIPData: common.GeoIPData{Country: "C1", ASN: "A1"},
  303. CommonCompartmentIDs: basicCommonCompartmentIDs,
  304. }
  305. geoIPData2 := &MatchProperties{
  306. GeoIPData: common.GeoIPData{Country: "C2", ASN: "A2"},
  307. CommonCompartmentIDs: basicCommonCompartmentIDs,
  308. }
  309. go proxyFunc(proxyResultChan, proxyIP, geoIPData1, 10*time.Millisecond, nil, true)
  310. go clientFunc(clientResultChan, clientIP, geoIPData2, 10*time.Millisecond)
  311. err = <-proxyResultChan
  312. if err != nil {
  313. return errors.Trace(err)
  314. }
  315. err = <-clientResultChan
  316. if err != nil {
  317. return errors.Trace(err)
  318. }
  319. // Test: answer error
  320. go proxyFunc(proxyResultChan, proxyIP, geoIPData1, 10*time.Millisecond, nil, false)
  321. go clientFunc(clientResultChan, clientIP, geoIPData2, 10*time.Millisecond)
  322. err = <-proxyResultChan
  323. if err != nil {
  324. return errors.Trace(err)
  325. }
  326. err = <-clientResultChan
  327. if err == nil || !strings.HasSuffix(err.Error(), "no answer") {
  328. return errors.Tracef("unexpected result: %v", err)
  329. }
  330. // Test: client is gone
  331. waitBeforeAnswer := make(chan struct{})
  332. go proxyFunc(proxyResultChan, proxyIP, geoIPData1, 100*time.Millisecond, waitBeforeAnswer, true)
  333. go clientFunc(clientResultChan, clientIP, geoIPData2, 10*time.Millisecond)
  334. err = <-clientResultChan
  335. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  336. return errors.Tracef("unexpected result: %v", err)
  337. }
  338. close(waitBeforeAnswer)
  339. err = <-proxyResultChan
  340. if err == nil || !strings.HasSuffix(err.Error(), "no client") {
  341. return errors.Tracef("unexpected result: %v", err)
  342. }
  343. // Test: no compartment match
  344. compartment1 := &MatchProperties{
  345. GeoIPData: geoIPData1.GeoIPData,
  346. CommonCompartmentIDs: []ID{makeID()},
  347. PersonalCompartmentIDs: []ID{makeID()},
  348. }
  349. compartment2 := &MatchProperties{
  350. GeoIPData: geoIPData2.GeoIPData,
  351. CommonCompartmentIDs: []ID{makeID()},
  352. PersonalCompartmentIDs: []ID{makeID()},
  353. }
  354. go proxyFunc(proxyResultChan, proxyIP, compartment1, 10*time.Millisecond, nil, true)
  355. go clientFunc(clientResultChan, clientIP, compartment2, 10*time.Millisecond)
  356. err = <-proxyResultChan
  357. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  358. return errors.Tracef("unexpected result: %v", err)
  359. }
  360. err = <-clientResultChan
  361. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  362. return errors.Tracef("unexpected result: %v", err)
  363. }
  364. // Test: common compartment match
  365. compartment1And2 := &MatchProperties{
  366. GeoIPData: geoIPData2.GeoIPData,
  367. CommonCompartmentIDs: []ID{compartment1.CommonCompartmentIDs[0], compartment2.CommonCompartmentIDs[0]},
  368. }
  369. go proxyFunc(proxyResultChan, proxyIP, compartment1, 10*time.Millisecond, nil, true)
  370. go clientFunc(clientResultChan, clientIP, compartment1And2, 10*time.Millisecond)
  371. err = <-proxyResultChan
  372. if err != nil {
  373. return errors.Trace(err)
  374. }
  375. err = <-clientResultChan
  376. if err != nil {
  377. return errors.Trace(err)
  378. }
  379. // Test: personal compartment match
  380. compartment1And2 = &MatchProperties{
  381. GeoIPData: geoIPData2.GeoIPData,
  382. PersonalCompartmentIDs: []ID{compartment1.PersonalCompartmentIDs[0], compartment2.PersonalCompartmentIDs[0]},
  383. }
  384. go proxyFunc(proxyResultChan, proxyIP, compartment1, 10*time.Millisecond, nil, true)
  385. go clientFunc(clientResultChan, clientIP, compartment1And2, 10*time.Millisecond)
  386. err = <-proxyResultChan
  387. if err != nil {
  388. return errors.Trace(err)
  389. }
  390. err = <-clientResultChan
  391. if err != nil {
  392. return errors.Trace(err)
  393. }
  394. // Test: personal compartment preferred match
  395. compartment1Common := &MatchProperties{
  396. GeoIPData: geoIPData1.GeoIPData,
  397. CommonCompartmentIDs: []ID{compartment1.CommonCompartmentIDs[0]},
  398. }
  399. compartment1Personal := &MatchProperties{
  400. GeoIPData: geoIPData1.GeoIPData,
  401. PersonalCompartmentIDs: []ID{compartment1.PersonalCompartmentIDs[0]},
  402. }
  403. compartment1CommonAndPersonal := &MatchProperties{
  404. GeoIPData: geoIPData2.GeoIPData,
  405. CommonCompartmentIDs: []ID{compartment1.CommonCompartmentIDs[0]},
  406. PersonalCompartmentIDs: []ID{compartment1.PersonalCompartmentIDs[0]},
  407. }
  408. client1ResultChan := make(chan error)
  409. client2ResultChan := make(chan error)
  410. proxy1ResultChan := make(chan error)
  411. proxy2ResultChan := make(chan error)
  412. go proxyFunc(proxy1ResultChan, proxyIP, compartment1Common, 10*time.Millisecond, nil, true)
  413. go proxyFunc(proxy2ResultChan, proxyIP, compartment1Personal, 10*time.Millisecond, nil, true)
  414. time.Sleep(5 * time.Millisecond) // Hack to ensure both proxies are enqueued
  415. go clientFunc(client1ResultChan, clientIP, compartment1CommonAndPersonal, 10*time.Millisecond)
  416. err = <-proxy1ResultChan
  417. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  418. return errors.Tracef("unexpected result: %v", err)
  419. }
  420. // proxy2 should match since it has the preferred personal compartment ID
  421. err = <-proxy2ResultChan
  422. if err != nil {
  423. return errors.Trace(err)
  424. }
  425. err = <-client1ResultChan
  426. if err != nil {
  427. return errors.Trace(err)
  428. }
  429. // Test: no same-ASN match
  430. go proxyFunc(proxyResultChan, proxyIP, geoIPData1, 10*time.Millisecond, nil, true)
  431. go clientFunc(clientResultChan, clientIP, geoIPData1, 10*time.Millisecond)
  432. err = <-proxyResultChan
  433. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  434. return errors.Tracef("unexpected result: %v", err)
  435. }
  436. err = <-clientResultChan
  437. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  438. return errors.Tracef("unexpected result: %v", err)
  439. }
  440. // Test: proxy preferred NAT match
  441. client1Properties := &MatchProperties{
  442. GeoIPData: common.GeoIPData{Country: "C1", ASN: "A1"},
  443. NATType: NATTypeFullCone,
  444. CommonCompartmentIDs: basicCommonCompartmentIDs,
  445. }
  446. client2Properties := &MatchProperties{
  447. GeoIPData: common.GeoIPData{Country: "C2", ASN: "A2"},
  448. NATType: NATTypeSymmetric,
  449. CommonCompartmentIDs: basicCommonCompartmentIDs,
  450. }
  451. proxy1Properties := &MatchProperties{
  452. GeoIPData: common.GeoIPData{Country: "C3", ASN: "A3"},
  453. NATType: NATTypeNone,
  454. CommonCompartmentIDs: basicCommonCompartmentIDs,
  455. }
  456. proxy2Properties := &MatchProperties{
  457. GeoIPData: common.GeoIPData{Country: "C4", ASN: "A4"},
  458. NATType: NATTypeSymmetric,
  459. CommonCompartmentIDs: basicCommonCompartmentIDs,
  460. }
  461. go proxyFunc(proxy1ResultChan, proxyIP, proxy1Properties, 10*time.Millisecond, nil, true)
  462. go proxyFunc(proxy2ResultChan, proxyIP, proxy2Properties, 10*time.Millisecond, nil, true)
  463. time.Sleep(5 * time.Millisecond) // Hack to ensure both proxies are enqueued
  464. go clientFunc(client1ResultChan, clientIP, client1Properties, 10*time.Millisecond)
  465. err = <-proxy1ResultChan
  466. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  467. return errors.Tracef("unexpected result: %v", err)
  468. }
  469. // proxy2 should match since it's the preferred NAT match
  470. err = <-proxy2ResultChan
  471. if err != nil {
  472. return errors.Trace(err)
  473. }
  474. err = <-client1ResultChan
  475. if err != nil {
  476. return errors.Trace(err)
  477. }
  478. // Test: client preferred NAT match
  479. // Limitation: the current Matcher.matchAllOffers logic matches the first
  480. // enqueued client against the best proxy match, regardless of whether
  481. // there is another client in the queue that's a better match for that
  482. // proxy. As a result, this test only passes when the preferred matching
  483. // client is enqueued first, and the test is currently of limited utility.
  484. go clientFunc(client2ResultChan, clientIP, client2Properties, 20*time.Millisecond)
  485. time.Sleep(5 * time.Millisecond) // Hack to client is enqueued
  486. go clientFunc(client1ResultChan, clientIP, client1Properties, 20*time.Millisecond)
  487. time.Sleep(5 * time.Millisecond) // Hack to client is enqueued
  488. go proxyFunc(proxy1ResultChan, proxyIP, proxy1Properties, 20*time.Millisecond, nil, true)
  489. err = <-proxy1ResultChan
  490. if err != nil {
  491. return errors.Trace(err)
  492. }
  493. err = <-client1ResultChan
  494. if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
  495. return errors.Tracef("unexpected result: %v", err)
  496. }
  497. // client2 should match since it's the preferred NAT match
  498. err = <-client2ResultChan
  499. if err != nil {
  500. return errors.Trace(err)
  501. }
  502. // Test: many matches
  503. // Reduce test log noise for this phase of the test
  504. logger.SetLogLevelDebug(false)
  505. matchCount := 10000
  506. proxyCount := matchCount
  507. clientCount := matchCount
  508. // Buffered so no goroutine will block reporting result
  509. proxyResultChan = make(chan error, matchCount)
  510. clientResultChan = make(chan error, matchCount)
  511. for proxyCount > 0 || clientCount > 0 {
  512. // Don't simply alternate enqueuing a proxy and a client
  513. if proxyCount > 0 && (clientCount == 0 || prng.FlipCoin()) {
  514. go proxyFunc(proxyResultChan, randomIPAddress(), geoIPData1, 10*time.Second, nil, true)
  515. proxyCount -= 1
  516. } else if clientCount > 0 {
  517. go clientFunc(clientResultChan, randomIPAddress(), geoIPData2, 10*time.Second)
  518. clientCount -= 1
  519. }
  520. }
  521. for i := 0; i < matchCount; i++ {
  522. err = <-proxyResultChan
  523. if err != nil {
  524. return errors.Trace(err)
  525. }
  526. err = <-clientResultChan
  527. if err != nil {
  528. return errors.Trace(err)
  529. }
  530. }
  531. return nil
  532. }
  533. func randomIPAddress() string {
  534. return fmt.Sprintf("%d.%d.%d.%d",
  535. prng.Range(0, 255),
  536. prng.Range(0, 255),
  537. prng.Range(0, 255),
  538. prng.Range(0, 255))
  539. }