Canonical Voices

mandel

In the ubuntu download manager we are using the new connection style syntax so that if there are errors in the signal connections we will be notified at compile time. However, in recent versions of udm we have noticed that the udm tests that ensure that the qt signals are emitted correctly have started failing randomly in the build servers.

As it can be seen in the following build logs the compilation does finish with no errors but the tests raise errors at runtime (an assert was added for each of the connect calls in the project):

Some of the errors between the diff archs are the same but this feels like a coincidence. The unity-scope-click package project has had the same issue and has solved it in the following way:

123
124
125
126
127
128
129
130
131
132
 
    // NOTE: using SIGNAL/SLOT macros here because new-style
    // connections are flaky on ARM.
    c = connect(impl->systemDownloadManager.data(), SIGNAL(downloadCreated(Download*)),
                this, SLOT(handleDownloadCreated(Download*)));
 
    if (!c) {
        qDebug() << "failed to connect to systemDownloadManager::downloadCreated";
 
    }

I am not the only one that have encoutered this bug within canonical (check out this bug). Apprently -Bsymbolic breaks PMF (Pointer to Member Function) comparison under ARM as it was reported in linaro. As it is explained in the Linaro mailing list a workaround to this (since the correct way would be to fix the linker) is to build with PIE support. The Qt guys have decided to drop -Bsymbolic* on anything but x86 and x86-64. I hope all this info help others that might find the same problem.

Read more
mandel

This is a small tip for those thos want to use QTest and Google Mock. To ensure that the expectations are check at the end of the execution of the test function and that errors are correctly reported you have to check the expectaions manually and pass the results to a QVERIFY macro. The following examples should be good to get you started:

1
2
3
4
5
6
7
8
9
10
11
void
TestBaseDownload::testStartQueued() {
    QScopedPointer<MockDownload> down(
        new MockDownload(_id, _path, _isConfined, _rootPath, _url,
            _metadata, _headers));
    down->start();
    EXPECT_CALL(*down.data(), startDownload())
            .Times(0);
 
    QVERIFY(Mock::VerifyAndClearExpectations(down.data()));
}

The important line to look at in the example is the following:

1
QVERIFY(Mock::VerifyAndClearExpectations(down.data()));

There we are passing the result of Mock::VerifyAndClearExpectations, where VerifyAndClearExpectations verifies and removes the expectations on a mocked object and returls a bool if it was successful. This way if the expectations are not met the QTest will fail.

For those interested in the error output it would be somthing of the following stype:

home/mandel/Canonical/udm/upload-interface/ubuntu-download-manager-tests/test_base_download.cpp:135: Failure
Mock function called more times than expected - returning directly.
    Function call: cancelDownload()
         Expected: to be never called
           Actual: called once - over-saturated and active
FAIL!  : TestBaseDownload::testCancelNotQueued() 'Mock::VerifyAndClearExpectations(down.data())' returned FALSE. ()
   Loc: [/home/mandel/Canonical/udm/upload-interface/ubuntu-download-manager-tests/test_base_download.cpp(139)]

Read more
mandel

You might have had the following error in your dbus daemon at some point and said to yoursefl WTF???

process 1288: arguments to dbus_message_set_error_name() were incorrect, assertion "error_name == NULL
   || _dbus_check_is_valid_error_name (error_name)" failed in file dbus-message.c line 2900.

Well, you are not the only one and I might be able to point you to the correct direction, your code is probably returnning a QDBusError that you created using the QDBusError::Other enum. The problem here is that the enum value your are using only indicates that the error name is not known and therefore cannot be match to a value in the QDBusError enum. When you use that enumerator the message created does have an incorrect name as follows:

QDBusMessage(type=Error, service="", error name="other", error message="msg", signature="", contents=() ) 

And “other” is, of course, not a valid DBus name and thefore the app crashes. Easies way to solve it, create a correct DBusError ;)

Read more
mandel

Ok, imaging that you are working with Qt 5 and using the new way to connect signals, lets for example say we are working with QNetworkReply and we want to have a slot for the QNetworkReply::error signals that takes a QNetworkReply::NetworkError, the way to do it is the following:

1
2
3
4
 
connect(_reply, static_cast<void(QNetworkReply::*)
    (QNetworkReply::NetworkError)>(&QNetworkReply::error),
        this, &MyClass::onNetworkError)

The static_cast is helping the compiler know what method (the signals or the actual method) you are talking about. I know, it is not nice at all but works at compile time better than getting a qWarning at runtime.

The problem is that without the help the compiler does not know what method error you are talking about :-/

Read more
mandel

I the last few months I have been working on the Ubuntu Download Manager, one of The Big Rocks of August. The u-d-m provide a dbus service that allows applications to request downloads to be performed, to such download requests it adds some nice features that a user on a mobile phone, and probably a desktop, is interested to have. Some of those features are:

  • Apparmor isolation per download. That means that only you application can interact with its downloads.
  • Pause/Resume downloads
  • Autodetect network connection.
  • WIFI only downloads.
  • Hash check support.
  • Allow downloads to be performed while an application has been paused or killed.
  • Group downloads, where a bunch of files are provided and the different downloads are performed as a single atomic operation.

A download might seem a simple action to perform, right? Well, as soon as you start supporting all the above a single download operation becomes a fairly complicated matter. The following is a state machine that identifies the states of a download that would support such features:

Download

As you can see, it is a complicated matter and all these has to be tested and check by the QA team. By providing u-d-m (and later a client library to use approach in C and in the Ubuntu SDK, I’m terribly sorry but I did not have the time to finish it on time for the release) we are helping developers to perform simple downloads with robust code and do not worry about all the corner cases. Performing a download is as simple as requesting it and listen to the different signals. This kind of service is also provided by FirefoxOs, WEbOs and Tizan (but no in IOS or SailFish) but I believe we are doing a better job at exposing a richer API. Of course all this is open source and at least our friend at Jolla (and I really mean friends, I think they are doing an awesome work!!! and competition + collaboration is great).

In the following days I’ll be posting on hot to use the API via C, C++ and DBus.

Read more
mandel

Long time no posts!

I have not updating this page lately for a very simple reason: TO MUCH TO CODE.

After a crazy amount of work to push the Ubuntu Download Manager to be the centralized daemon used to perform downloads in Ubuntu Touch I have some more time to write. In the following weeks I’m going to be focusing on explaining some issues and patterns I have found using Qt while developing u-d-m (short of ubuntu download manager from now on).

PS: Is quite embarrasing that my last post was the ‘She got me dancing’ videoclip… you should take a look nevertheless :)

Read more
mandel

I have recently been doing some work with Qt and DBus and I got stuck a little on how to correctly send a {sv} over DBus. Either my google-fu is terrible or there are not many examples on how to do this, therefore here is a small static method that will return a {sv} that can be send via DBus without getting a wrong parameters error:

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
 
#ifndef DBUS_HELPER_H
#define DBUS_HELPER_H
 
#include 
#include 
#include 
#include 
 
typedef QHash DBusStringHash;
class DBusHelper : public QObject
{
    Q_OBJECT
public:
    static int DBUS_STRING_MAP_ID;
 
    explicit DBusHelper(QObject *parent = 0);
 
    static class _init
    {
        public:
            _init()
            {
                // diff actions to init
                qRegisterMetaType("DBusStringHash");
                qDBusRegisterMetaType();
                DBUS_STRING_MAP_ID = QMetaType::type("DBusStringHash");
            }
    } _initializer;
 
    static QVariant getVariant(DBusStringHash hash);
 
};
 
Q_DECLARE_METATYPE(DBusStringHash)
#endif // DBUS_HELPER_H
1
2
3
4
5
6
7
8
9
10
11
12
13
// required for the init
int DBusHelper::DBUS_STRING_MAP_ID = 0;
DBusHelper::_init DBusHelper::_initializer;
 
DBusHelper::DBusHelper(QObject *parent) :
    QObject(parent)
{
}
 
QVariant DBusHelper::getVariant(DBusStringHash hash)
{
    return QVariant(DBUS_STRING_MAP_ID, &amp;hash);
}

I added the init trick so that there is no need to manually register the types. I hope it helps!

Read more
mandel

Following the small work I’m doing to get better at golang here is another piece of work I have done to add a golang binding to one of the C libraries I’m depending on. The following allows to use the libaccounts-glib library from 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
package main
 
/*
#cgo pkg-config: libaccounts-glib
#include <stdlib.h>
#include <glib.h>
#include <libaccounts-glib/accounts-glib.h>
 
static inline char* to_charptr(const gchar* s) { return (char*)s; }
 
static inline char* to_charptr_from_ptr(gpointer s) { return (char*)s; }
 
static inline gchar* to_gcharptr(const char* s) { return (gchar*)s; }
 
static inline AgService* to_AgService(void* o) { return (AgService *)o; }
 
static inline AgAccountService* to_AgAccountService(void* o) { return (AgAccountService *)o; }
 
static inline AgApplication* to_AgApplication(void* o) { return (AgApplication *)o; }
 
static inline AgProvider* to_AgProvider(void* o) { return (AgProvider *)o; }
 
static inline AgServiceType* to_AgServiceType(void* o) { return (AgServiceType *)o; }
 
static inline void free_string(char* s) { free(s); }
 
static inline AgAccountId to_AgAccountId(guint i) { return (AgAccountId) i; }
 
static inline guint to_guint(gpointer i) { return (guint)i; }
 
static GError* to_error(void* err) { return (GError*)err; }
 
extern void go_account_notify_cb(void* func, AgAccount* acc, gchar* key);
 
static void account_notify_cb(AgAccount* acc, gchar* key, gpointer func){
    go_account_notify_cb((void*)func, acc, key);
}
 
static AgAccountWatch watch_dir(AgAccount* acc, gchar* key, void* func) {
    return ag_account_watch_dir(acc, key,
        (AgAccountNotifyCb)account_notify_cb, func);
}
 
static AgAccountWatch watch_key(AgAccount* acc, gchar* key, void* func) {
    return ag_account_watch_key(acc, key,
        (AgAccountNotifyCb)account_notify_cb, func);
}
 
*/
import "C"
 
import (
    "os"
    "fmt"
    "unsafe"
    "github.com/mattn/go-gtk/gtk"
)
 
func GetStringFromGCharPtr(data *C.gchar) string {
    if data == nil {
        return ""
    }
    return C.GoString(C.to_charptr(data))
}
 
func GetServicesFromList(services *C.GList, length C.guint) ([]*Service) {
    result := make([]*Service, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(services, n)
        pointer := unsafe.Pointer(data)
        service := &Service{serv:C.to_AgService(pointer)}
        result[uint(n)] = service
    }
    return result
}
 
func GetAccountServicesFromList(services *C.GList,
    length C.guint) ([]*AccountService) {
 
    result := make([]*AccountService, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(services, n)
        pointer := unsafe.Pointer(data)
        service := &AccountService{acc:C.to_AgAccountService(pointer)}
        result[uint(n)] = service
    }
    return result
}
 
func GetIdsFromList(ids *C.GList, length C.guint) ([]uint32) {
    result := make([]uint32, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(ids, n)
        result[uint(n)] = uint32(C.to_guint(data))
    }
 
    return result
}
 
// =============================== GError =====================================
 
type Error struct {
    GError *C.GError
}
 
func (v *Error) Error() string {
    return v.Message()
}
 
func (v *Error) Message() string {
    if unsafe.Pointer(v.GError) == nil || unsafe.Pointer(v.GError.message) == nil {
        return ""
    }
    return C.GoString(C.to_charptr(v.GError.message))
}
 
func ErrorFromNative(err unsafe.Pointer) *Error {
    return &Error{C.to_error(err)}
}
 
// ============================== Application ====================================
 
type Application struct {
    app *C.AgApplication
}
 
func (app *Application) GetDescription () string {
    return GetStringFromGCharPtr(C.ag_application_get_description(app.app))
}
 
func (app *Application) GetName () string {
    return GetStringFromGCharPtr(C.ag_application_get_name(app.app))
}
 
func (app *Application) GetServiceUsage (service *Service) string {
    return GetStringFromGCharPtr(
        C.ag_application_get_service_usage(app.app, service.serv))
}
 
func (app *Application) Unref () {
    C.ag_application_unref(C.to_AgApplication(unsafe.Pointer(app.app)))
}
 
// =============================== Provider ======================================
 
type Provider struct {
    prov *C.AgProvider
}
 
func (prov *Provider) GetDisplayName () string {
    return GetStringFromGCharPtr(C.ag_provider_get_display_name(prov.prov))
}
 
func (prov *Provider) GetDescription () string {
    return GetStringFromGCharPtr(C.ag_provider_get_description(prov.prov))
}
 
func (prov *Provider) GetName () string {
    return GetStringFromGCharPtr(C.ag_provider_get_name(prov.prov))
}
 
func (prov *Provider) Unref () {
    C.ag_provider_unref(C.to_AgProvider(unsafe.Pointer(prov.prov)))
}
 
// ================================ Manager ======================================
 
type Manager struct {
    man *C.AgManager
}
 
func (man *Manager) CreateAccount (provider_name string) *Account {
    provider_str := C.CString(provider_name)
    defer C.free_string(provider_str)
 
    acc := C.ag_manager_create_account(man.man, C.to_gcharptr(provider_str))
    return &Account{acc:acc}
}
 
func (man *Manager) GetAccount (id uint32) *Account {
    acc := C.ag_manager_get_account(man.man, C.to_AgAccountId(C.guint(id)))
    return &Account{acc:acc}
}
 
func (man *Manager) GetAccountServices () ([]*AccountService, uint) {
    services := C.ag_manager_get_account_services(man.man)
    defer C.g_list_free(services)
 
    length := C.g_list_length(services)
    result := GetAccountServicesFromList(services, length)
 
    return result, uint(length)
}
 
func (man *Manager) GetApplication (app_name string) *Application {
    app_name_str := C.CString(app_name)
    defer C.free_string(app_name_str)
 
    app:= C.ag_manager_get_application(man.man, C.to_gcharptr(app_name_str))
    return &Application{app:app}
}
 
func (man *Manager) GetEnabledAccountServices() ([]*AccountService, uint) {
    services := C.ag_manager_get_enabled_account_services(man.man)
    defer C.g_list_free(services)
 
    length := C.g_list_length(services)
    result := GetAccountServicesFromList(services, length)
 
    return result, uint(length)
}
 
func (man *Manager) GetProvider(provider_name string) *Provider {
    provider_name_str := C.CString(provider_name)
    defer C.free_string(provider_name_str)
 
    provider := C.ag_manager_get_provider(man.man, C.to_gcharptr(provider_name_str))
    return &Provider{prov:provider}
}
 
func (man *Manager) GetService (service_name string) *Service {
    service_name_str := C.CString(service_name)
    defer C.free_string(service_name_str)
 
    service := C.ag_manager_get_service(man.man, C.to_gcharptr(service_name_str))
    return &Service{serv:service}
}
 
func (man *Manager) GetServiceType () string {
    return GetStringFromGCharPtr(C.ag_manager_get_service_type(man.man))
}
 
func (man *Manager) List() ([]uint32, uint) {
    account_ids := C.ag_manager_list(man.man)
    defer C.g_list_free(account_ids)
 
    length := C.g_list_length(account_ids)
    result := GetIdsFromList(account_ids, length)
 
    return result, uint(length)
}
 
func (man *Manager) ListApplicationsByService (
    service *Service) ([]*Application, uint) {
 
    apps := C.ag_manager_list_applications_by_service(
        man.man, service.serv)
    defer C.g_list_free(apps)
 
    length := C.g_list_length(apps)
    result := make([]*Application, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(apps, n)
        pointer := unsafe.Pointer(data)
        app := &Application{app:C.to_AgApplication(pointer)}
        result[uint(n)] = app
    }
 
    return result, uint(length)
}
 
func (man *Manager) ListByServiceType(
    service_type string) ([]uint32, uint) {
 
    service_type_str := C.CString(service_type)
    defer C.free_string(service_type_str)
 
    service_ids := C.ag_manager_list_by_service_type(
        man.man, C.to_gcharptr(service_type_str))
    defer C.g_list_free(service_ids)
 
    length := C.g_list_length(service_ids)
    result := GetIdsFromList(service_ids, length)
 
    return result, uint(length)
}
 
func (man *Manager) ListEnabled () ([]uint32, uint) {
    account_ids := C.ag_manager_list_enabled(man.man)
    defer C.g_list_free(account_ids)
 
    length := C.g_list_length(account_ids)
    result := GetIdsFromList(account_ids, length)
 
    return result, uint(length)
}
 
func (man *Manager) ListEnabledByServiceType (
    service_type string) ([]uint32, uint) {
    service_type_str := C.CString(service_type)
    defer C.free_string(service_type_str)
 
    account_ids := C.ag_manager_list_enabled_by_service_type(man.man,
        C.to_gcharptr(service_type_str))
 
    length := C.g_list_length(account_ids)
    result := GetIdsFromList(account_ids, length)
 
    return result, uint(length)
}
 
func (man *Manager) ListProviders () ([]*Provider, uint) {
 
    providers := C.ag_manager_list_providers(man.man)
    defer C.g_list_free(providers)
 
    length := C.g_list_length(providers)
    result := make([]*Provider, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(providers, n)
        pointer := unsafe.Pointer(data)
        app := &Provider{prov:C.to_AgProvider(pointer)}
        result[uint(n)] = app
    }
 
    return result, uint(length)
}
 
func (man *Manager) ListServiceTypes () ([]*ServiceType, uint) {
 
    services := C.ag_manager_list_service_types(man.man)
    defer C.g_list_free(services)
 
    length := C.g_list_length(services)
    result := make([]*ServiceType, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(services, n)
        pointer := unsafe.Pointer(data)
        app := &ServiceType{serv_type:C.to_AgServiceType(pointer)}
        result[uint(n)] = app
    }
 
    return result, uint(length)
}
 
func (man *Manager) ListServices () ([]*Service, uint) {
    services := C.ag_manager_list_services(man.man)
    defer C.g_list_free(services)
 
    length := C.g_list_length(services)
    result := make([]*Service, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(services, n)
        pointer := unsafe.Pointer(data)
        app := &Service{serv:C.to_AgService(pointer)}
        result[uint(n)] = app
    }
 
    return result, uint(length)
}
 
func (man *Manager) ListServicesByType (
    service_type string) ([]*Service, uint) {
 
    service_type_str := C.CString(service_type)
    defer C.free_string(service_type_str)
 
    services := C.ag_manager_list_services_by_type(
        man.man, C.to_gcharptr(service_type_str))
    defer C.g_list_free(services)
 
    length := C.g_list_length(services)
    result := make([]*Service, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(services, n)
        pointer := unsafe.Pointer(data)
        app := &Service{serv:C.to_AgService(pointer)}
        result[uint(n)] = app
    }
 
    return result, uint(length)
}
 
func (man *Manager) LoadAccount (account_id uint32) (*Account, error) {
    var gerror *C.GError
 
    id := C.to_AgAccountId(C.guint(account_id))
    account := C.ag_manager_load_account(man.man, id, &gerror)
 
    if gerror != nil {
        err := ErrorFromNative(unsafe.Pointer(gerror))
        return nil, err
    }
    return &Account{acc:account}, nil
}
 
func (man *Manager) LoadServiceType (
    service_type string) *ServiceType {
 
    service_type_str := C.CString(service_type)
    defer C.free_string(service_type_str)
 
    stype := C.ag_manager_load_service_type(man.man,
        C.to_gcharptr(service_type_str))
    return &ServiceType{serv_type:stype}
}
 
func (man *Manager) Unref () {
    C.g_object_unref(C.gpointer(man.man))
}
 
func NewManager () *Manager {
    return &Manager{man:C.ag_manager_new()}
}
 
func NewManagerForServiceType (service_type string) *Manager {
 
    service_type_str := C.CString(service_type)
    defer C.free_string(service_type_str)
 
    return &Manager{man:C.ag_manager_new_for_service_type(
        C.to_gcharptr(service_type_str))}
}
 
// ============================= Account Service =================================
 
type AccountService struct {
    acc *C.AgAccountService
}
 
func (acc *AccountService) GetAccount () *Account {
    return &Account{C.ag_account_service_get_account(acc.acc)}
}
 
func (acc *AccountService) GetService () *Service {
    return &Service{C.ag_account_service_get_service(acc.acc)}
}
 
func (acc *AccountService) GetEnabled () bool {
    return C.TRUE == C.ag_account_service_get_enabled(acc.acc)
}
 
// ================================ Service ======================================
 
type Service struct {
    serv *C.AgService
}
 
func (serv *Service) GetDisplayName () string {
    return GetStringFromGCharPtr(C.ag_service_get_display_name(serv.serv))
}
 
func (serv *Service) GetName () string {
    return GetStringFromGCharPtr(C.ag_service_get_name(serv.serv))
}
 
func (serv *Service) GetDescription () string {
    return GetStringFromGCharPtr(C.ag_service_get_description(serv.serv))
}
 
func (serv *Service) GetProvider () string {
    return GetStringFromGCharPtr(C.ag_service_get_provider(serv.serv))
}
 
func (serv *Service) GetServiceType () string {
    return GetStringFromGCharPtr(C.ag_service_get_service_type(serv.serv))
}
 
func (serv *Service) GetTags () ([]string, uint) {
 
    tags := C.ag_service_get_tags(serv.serv)
    defer C.g_list_free(tags)
 
    length := C.g_list_length(tags)
    result := make([]string, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(tags, n)
        pointer := C.to_charptr_from_ptr(data)
        result[uint(n)] = C.GoString(pointer)
    }
 
    return result, uint(length)
}
 
func (serv *Service) HasTag (tag string) bool {
    tag_str := C.CString(tag)
    defer C.free_string(tag_str)
 
    has_tag := C.ag_service_has_tag(serv.serv, C.to_gcharptr(tag_str))
    return C.TRUE == has_tag
}
 
func (serv *Service) Unref () {
    C.ag_service_unref(C.to_AgService(unsafe.Pointer(serv.serv)))
}
 
// ============================== ServiceType ====================================
 
type ServiceType struct {
    serv_type *C.AgServiceType
}
 
func (serv *ServiceType) GetDisplayName () string {
    return GetStringFromGCharPtr(
        C.ag_service_type_get_display_name(serv.serv_type))
}
 
func (serv *ServiceType) GetDescription () string {
    return GetStringFromGCharPtr(
        C.ag_service_type_get_description(serv.serv_type))
}
 
func (serv *ServiceType) GetName () string {
    return GetStringFromGCharPtr(C.ag_service_type_get_name(serv.serv_type))
}
 
func (serv *ServiceType) GetTags () ([]string, uint) {
 
    tags := C.ag_service_get_tags(serv.serv_type)
    defer C.g_list_free(tags)
 
    length := C.g_list_length(tags)
    result := make([]string, length)
 
    for n := C.guint(0); n < length; n++ {
        data := C.g_list_nth_data(tags, n)
        pointer := C.to_charptr_from_ptr(data)
        result[uint(n)] = C.GoString(pointer)
    }
 
    return result, uint(length)
}
 
func (serv *ServiceType) HasTag (tag string) bool {
    tag_str := C.CString(tag)
    defer C.free_string(tag_str)
 
    has_tag := C.ag_service_has_tag(serv.serv_type, C.to_gcharptr(tag_str))
    return C.TRUE == has_tag
}
 
func (serv *ServiceType) Unref () {
    C.ag_service_type_unref(C.to_AgServiceType(
        unsafe.Pointer(serv.serv_type)))
}
 
// ============================== Account Watch ==================================
 
type AccountWatch struct {
    watch *C.AgAccountWatch
}
 
func (watch *AccountWatch ) Unref () {
    C.g_object_unref(C.gpointer(watch.watch))
}
 
// ================================ Account ======================================
 
type AccountAsyncStoreCallback func(acc *Account, success bool, err error)
 
type AccountAsyncWatchDirCallback func(acc *Account, key string)
 
type Account struct {
    acc *C.AgAccount
}
 
//export go_account_notify_cb
func go_account_notify_cb(pcallback unsafe.Pointer, gaccount *C.AgAccount,
    gkey *C.gchar) {
 
    account := &Account{acc:gaccount}
    key := C.GoString(C.to_charptr(gkey))
    callback := *(*func(*Account, string))(pcallback)
    callback(account, key)
}
 
func (acc *Account) Delete () {
    C.ag_account_delete(acc.acc)
}
 
func (acc *Account) GetDisplayName () string {
    return GetStringFromGCharPtr(C.ag_account_get_display_name(acc.acc))
}
 
func (acc *Account) GetEnabled () bool {
    return C.TRUE == C.ag_account_get_enabled(acc.acc)
}
 
func (acc *Account) GetManager () *Manager {
    return &Manager{man:C.ag_account_get_manager(acc.acc)}
}
 
func (acc *Account) GetProviderName () string {
    return GetStringFromGCharPtr(C.ag_account_get_provider_name(acc.acc))
}
 
func (acc *Account) GetSelectedService () *Service {
   return &Service{serv:C.ag_account_get_selected_service(acc.acc)}
}
 
func (acc *Account) ListEnabledServices () ([]*Service, uint) {
    services := C.ag_account_list_enabled_services(acc.acc)
    defer C.g_list_free(services)
 
    length := C.g_list_length(services)
    result := GetServicesFromList(services, length)
 
    return result, uint(length)
}
 
func (acc *Account) ListServices () ([]*Service, uint) {
    services := C.ag_account_list_services(acc.acc)
    defer C.g_list_free(services)
 
    length := C.g_list_length(services)
    result := GetServicesFromList(services, length)
 
    return result, uint(length)
}
 
func (acc *Account) ListServicesByType (service_type string) ([]*Service, uint) {
    service_type_str := C.CString(service_type)
    defer C.free_string(service_type_str)
 
    services := C.ag_account_list_services_by_type(acc.acc,
        C.to_gcharptr(service_type_str))
    defer C.g_list_free(services)
 
    length := C.g_list_length(services)
    result := GetServicesFromList(services, length)
 
    return result, uint(length)
}
 
func (acc *Account) RemoveWatch (watch *AccountWatch) {
    C.ag_account_remove_watch(acc.acc, *watch.watch)
}
 
func (acc *Account) SelectService (service *Service) {
    C.ag_account_select_service(acc.acc, service.serv)
}
 
func (acc *Account) SetDisplayName (name string) {
    name_str := C.CString(name)
    defer C.free_string(name_str)
    C.ag_account_set_display_name(acc.acc, C.to_gcharptr(name_str))
}
 
func (acc *Account) SetEnabled (enabled bool) {
    if enabled {
        C.ag_account_set_enabled(acc.acc, C.TRUE)
    } else {
        C.ag_account_set_enabled(acc.acc, C.FALSE)
    }
}
 
func (acc *Account) Sign (key string, token string) {
    key_str := C.CString(key)
    defer C.free_string(key_str)
 
    token_str := C.CString(token)
    defer C.free_string(token_str)
 
    C.ag_account_sign(acc.acc, C.to_gcharptr(key_str), C.to_gcharptr(token_str))
}
 
func (acc *Account) Store (callback AccountAsyncStoreCallback) {
 
    go func(){
        var gerror *C.GError
        C.ag_account_store_blocking(acc.acc, &gerror)
        if gerror != nil {
            err := ErrorFromNative(unsafe.Pointer(gerror))
            callback(acc, false, err)
            return
        }
        callback(acc, true, nil)
    }()
}
 
func (acc *Account) StoreSync () (bool, error){
    var gerror *C.GError
    C.ag_account_store_blocking(acc.acc, &gerror)
    if gerror != nil {
        err := ErrorFromNative(unsafe.Pointer(gerror))
        return false, err
    }
    return true, nil
}
 
func (acc *Account) SupportsService (service_type string) bool {
    service_type_str := C.CString(service_type)
    defer C.free_string(service_type_str)
 
    suported := C.ag_account_supports_service(acc.acc,
        C.to_gcharptr(service_type_str))
    return suported == C.TRUE
}
 
func (acc *Account) Verify (key string) (bool, string) {
    key_str := C.CString(key)
    defer C.free_string(key_str)
 
    var token *C.gchar
    verified := C.ag_account_verify(acc.acc, C.to_gcharptr(key_str), &token)
 
    return verified == C.TRUE, C.GoString(C.to_charptr(token))
}
 
func (acc *Account) VerifyWithTokens (key string, tokens []string) bool {
    key_str := C.CString(key)
    defer C.free_string(key_str)
 
    tokens_str := make([]*C.gchar, len(tokens))
    for n := 0; n < len(tokens); n++ {
        token := C.CString(tokens[n])
        defer C.free_string(token)
 
        tokens_str[n] = C.to_gcharptr(token)
    }
 
    verified := C.ag_account_verify_with_tokens(acc.acc, C.to_gcharptr(key_str),
        &tokens_str[0])
    return verified == C.TRUE
}
 
func (acc *Account) WatchDir (key string,
    callback AccountAsyncWatchDirCallback) *AccountWatch {
 
    key_str := C.CString(key)
    defer C.free_string(key_str)
 
    watch := C.watch_dir(acc.acc, C.to_gcharptr(key_str), unsafe.Pointer(&callback))
    return &AccountWatch{watch:&watch}
}
 
func (acc *Account) WatchKey (key string,
    callback AccountAsyncWatchDirCallback) *AccountWatch {
 
    key_str := C.CString(key)
    defer C.free_string(key_str)
 
    watch := C.watch_key(acc.acc, C.to_gcharptr(key_str), unsafe.Pointer(&callback))
    return &AccountWatch{watch:&watch}
}
 
func (acc *Account) Unref () {
    C.g_object_unref(C.gpointer(acc.acc))
}
 
func main() {
    fmt.Println("test")
    gtk.Init(&os.Args)
    go func () {
        manager := NewManagerForServiceType("microblogging")
        defer manager.Unref()
        services, length := manager.GetAccountServices()
        fmt.Printf("Got %d servicesn", length)
        for n := uint(0); n < length; n++ {
            s := services[n].GetService()
            fmt.Printf("Display Name: %sn", s.GetDisplayName())
            fmt.Printf("Name: %sn", s.GetName())
            fmt.Printf("Description: %sn", s.GetDescription())
            fmt.Printf("Provider: %sn", s.GetProvider())
        }
        gtk.MainQuit()
    }()
    gtk.Main()
}

By the way I have fixed a small mem leak I had int he goa bindings by adding the Unref methods so that the glib structures can be freed once you are done with them.

Read more
mandel

I have been playing a little with golang lately and I have decided to do a small desktop application to try an practice more. Because I wanted to integrate with the gnome-online-accounts API I had to do a small go interface for the API. I leave it here in the wild to see if people can spot errors with it or find it useful:

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
package main
 
 
/*
#define GOA_API_IS_SUBJECT_TO_CHANGE 1
#cgo pkg-config: goa-1.0
#include <stdlib.h>
#include <glib.h>
#include <gio/gio.h>
#include <goa/goa.h>
 
static inline gchar* to_gcharptr(const char* s) { return (gchar*)s; }
 
static inline char* to_charptr(const gchar* s) { return (char*)s; }
 
static inline GObject* to_GObject(void* o) { return G_OBJECT(o); }
 
static inline GoaObject* to_GoaObject(void* o) { return GOA_OBJECT(o); }
 
static GError* to_error(void* err) { return (GError*)err; }
 
static inline void free_string(char* s) { free(s); }
*/
import "C"
 
import (
    "os"
    "fmt"
    "errors"
    "unsafe"
    "github.com/mattn/go-gtk/glib"
    "github.com/mattn/go-gtk/gtk"
)
 
func InterfaceToVariant (data interface{}) (*C.GVariant, error) {
    switch data.(type) {
        case bool:
            if data.(bool) {
                return C.g_variant_new_boolean(C.TRUE), nil
            }
            return C.g_variant_new_boolean(C.FALSE), nil
        case byte:
            return C.g_variant_new_byte(C.guchar(data.(byte))), nil
        case int16:
            return C.g_variant_new_int16(C.gint16(data.(int16))), nil
        case uint16:
            return C.g_variant_new_uint16(C.guint16(data.(uint16))), nil
        case int32:
            return C.g_variant_new_int32(C.gint32(data.(int32))), nil
        case uint32:
            return C.g_variant_new_uint32(C.guint32(data.(uint32))), nil
        case int64:
            return C.g_variant_new_int64(C.gint64(data.(int64))), nil
        case uint64:
            return C.g_variant_new_uint64(C.guint64(data.(uint64))), nil
        case float32:
            return C.g_variant_new_double(C.gdouble(data.(float32))), nil
        case string:
            str := C.CString(data.(string))
            defer C.free_string(str)
            return C.g_variant_new_string(C.to_gcharptr(str)), nil
        default:
            return nil, errors.New("Could not covert interface to gvariant")
    }
    return nil, nil
}
 
// =============================== GError =====================================
 
type Error struct {
    GError *C.GError
}
 
func (v *Error) Error() string {
    return v.Message()
}
 
func (v *Error) Message() string {
    if unsafe.Pointer(v.GError) == nil || unsafe.Pointer(v.GError.message) == nil {
        return ""
    }
    return C.GoString(C.to_charptr(v.GError.message))
}
 
func ErrorFromNative(err unsafe.Pointer) *Error {
    return &Error{C.to_error(err)}
}
 
// ============================= Cancelable ===================================
 
type Cancellable struct {
    cancellable *C.GCancellable
}
 
func NewCancellable () (*Cancellable) {
    return &Cancellable{cancellable:C.g_cancellable_new()}
}
 
func (can *Cancellable) IsCancelled() bool {
    result := C.g_cancellable_is_cancelled(can.cancellable)
    if result != 0 {
        return true
    }
    return false
}
 
// TODO: somehow pass a gerror
//func (can *Cancellable) SetErrorIfCancelled () {
//}
 
func (can *Cancellable) GetFd () int32 {
    return int32(C.g_cancellable_get_fd(can.cancellable))
}
 
func (can *Cancellable) MakePollFd (poll *C.GPollFD) bool {
    return false
}
 
func (can *Cancellable) ReleaseFd () {
    C.g_cancellable_release_fd(can.cancellable)
}
 
func GetCurrent () *Cancellable {
    current := C.g_cancellable_get_current()
    return &Cancellable{cancellable:current}
}
 
func (can *Cancellable) PopCurrent () {
    C.g_cancellable_pop_current(can.cancellable)
}
 
func (can *Cancellable) PushCurrent () {
    C.g_cancellable_push_current(can.cancellable)
}
 
func (can *Cancellable) Reset () {
    C.g_cancellable_reset(can.cancellable)
}
 
func (can *Cancellable) Cancel () {
    C.g_cancellable_cancel(can.cancellable)
}
 
// TODO
//func (can *Cancellable) Connect () {
//}
 
//TODO
//func (can *Cancellable) Disonnect () {
//}
 
// ================================= Manager ===============================
 
type ManagerAsyncAddAccountCallback func(man *Manager, object_path string,
    err error)
 
type Manager struct {
    manager *C.GoaManager
}
 
func (man *Manager) CallAddAccount (provider string, identity string,
        presentation_identity string, credentials interface{}, details interface{},
        can *Cancellable, callback ManagerAsyncAddAccountCallback) {
 
    go func() {
        var cancellable *C.GCancellable
        if can != nil {
            cancellable = can.cancellable
        }
 
        provider_str := C.CString(provider)
        defer C.free_string(provider_str)
 
        identity_str := C.CString(identity)
        defer C.free_string(identity_str)
 
        presentation_identity_str := C.CString(presentation_identity)
        defer C.free_string(presentation_identity_str)
 
        cred, err := InterfaceToVariant(credentials)
        if err != nil {
            callback(man, "", err)
            return
        }
 
        det, err := InterfaceToVariant(details)
 
        if err != nil {
            callback(man, "", err)
            return
        }
 
        var gerror *C.GError
        var object_path *C.gchar
 
        C.goa_manager_call_add_account_sync(man.manager,
            C.to_gcharptr(provider_str), C.to_gcharptr(identity_str),
            C.to_gcharptr(presentation_identity_str), cred, det,
            &object_path, cancellable, &gerror)
 
        if gerror != nil {
            err := ErrorFromNative(unsafe.Pointer(gerror))
            callback(man, "", err)
            return
        }
        callback(man, C.GoString(C.to_charptr(object_path)), nil)
    }()
}
 
func (man *Manager) CallAddAccountSync (provider string, identity string,
        presentation_identity string, credentials interface{}, details interface{},
        can *Cancellable) (string, error) {
 
    var cancellable *C.GCancellable
    if can != nil {
        cancellable = can.cancellable
    }
 
    provider_str := C.CString(provider)
    defer C.free_string(provider_str)
 
    identity_str := C.CString(identity)
    defer C.free_string(identity_str)
 
    presentation_identity_str := C.CString(presentation_identity)
    defer C.free_string(presentation_identity_str)
 
    cred, err := InterfaceToVariant(credentials)
    if err != nil {
        return "", err
    }
 
    det, err := InterfaceToVariant(details)
 
    if err != nil {
        return "", err
    }
 
    var gerror *C.GError
    var object_path *C.gchar
 
    C.goa_manager_call_add_account_sync(man.manager,
        C.to_gcharptr(provider_str), C.to_gcharptr(identity_str),
        C.to_gcharptr(presentation_identity_str), cred, det,
        &object_path, cancellable, &gerror)
 
    if gerror != nil {
        err := ErrorFromNative(unsafe.Pointer(gerror))
        return "", err
    }
    return C.GoString(C.to_charptr(object_path)), nil
}
 
// ================================ Account ================================
 
type AccountAsyncEnsureCredentialsCallback func(man *Account,
    expires_in int32, err error)
 
type AccountAsyncRemoveCallback func(man *Account,
    err error)
 
type Account struct {
    account *C.GoaAccount
}
 
func (acc *Account) GetProviderType () string {
    provider := C.goa_account_dup_provider_type(acc.account)
    defer C.free(unsafe.Pointer(provider))
    return C.GoString(C.to_charptr(provider))
}
 
func (acc *Account) GetProviderName () string {
    provider := C.goa_account_dup_provider_name(acc.account)
    defer C.free(unsafe.Pointer(provider))
    return C.GoString(C.to_charptr(provider))
}
 
func (acc *Account) GetProviderIcon () string {
    provider := C.goa_account_dup_provider_icon(acc.account)
    defer C.free(unsafe.Pointer(provider))
    return C.GoString(C.to_charptr(provider))
}
 
func (acc *Account) GetId () string {
    id := C.goa_account_dup_id(acc.account)
    defer C.free(unsafe.Pointer(id))
    return C.GoString(C.to_charptr(id))
}
 
func (acc *Account) GetAttentionNeeded () bool {
    attention_needed := C.goa_account_get_attention_needed(acc.account)
    return C.TRUE == attention_needed
}
 
func (acc *Account) GetIdentity () string {
    identity := C.goa_account_dup_identity(acc.account)
    defer C.free(unsafe.Pointer(identity))
    return C.GoString(C.to_charptr(identity))
}
 
func (acc *Account) GetPresentationIdentity () string {
    identity := C.goa_account_dup_presentation_identity(acc.account)
    defer C.free(unsafe.Pointer(identity))
    return C.GoString(C.to_charptr(identity))
}
 
func (acc *Account) GetMailDisabled () bool {
    disabled := C.goa_account_get_mail_disabled(acc.account)
    return C.TRUE == disabled
}
 
func (acc *Account) GetCalendarDisabled () bool {
    disabled := C.goa_account_get_calendar_disabled(acc.account)
    return C.TRUE == disabled
}
 
func (acc *Account) GetContactsDisabled () bool {
    disabled := C.goa_account_get_contacts_disabled(acc.account)
    return C.TRUE == disabled
}
 
func (acc *Account) GetChatDisabled  () bool {
    disabled := C.goa_account_get_chat_disabled(acc.account)
    return C.TRUE == disabled
}
 
func (acc *Account) GetDocumentsDisabled () bool {
    disabled := C.goa_account_get_documents_disabled(acc.account)
    return C.TRUE == disabled
}
 
func (acc *Account) CallEnsureCredentials (can *Cancellable,
    callback AccountAsyncEnsureCredentialsCallback) {
 
    go func() {
        var cancellable *C.GCancellable
        if can != nil {
            cancellable = can.cancellable
        }
        var gerror *C.GError
        var gint C.gint
 
        C.goa_account_call_ensure_credentials_sync(acc.account, &gint,
            cancellable, &gerror)
 
        if gerror != nil {
            err := ErrorFromNative(unsafe.Pointer(gerror))
            callback(acc, 0, err)
            return
        }
        callback(acc, int32(gint), nil)
    }()
}
 
func (acc *Account) CallSyncEnsureCredentials (
    can *Cancellable) (int32, error) {
 
    var cancellable *C.GCancellable
    if can != nil {
        cancellable = can.cancellable
    }
    var gerror *C.GError
    var gint C.gint
 
    C.goa_account_call_ensure_credentials_sync(acc.account, &gint,
        cancellable, &gerror)
 
    if gerror != nil {
        err := ErrorFromNative(unsafe.Pointer(gerror))
        return 0, err
    }
    return int32(gint), nil
}
 
func (acc *Account) Remove (can *Cancellable,
    callback AccountAsyncRemoveCallback) {
 
    go func() {
 
        var cancellable *C.GCancellable
        if can != nil {
            cancellable = can.cancellable
        }
        var gerror *C.GError
        C.goa_account_call_remove_sync(acc.account, cancellable, &gerror)
        if gerror != nil {
            err := ErrorFromNative(unsafe.Pointer(gerror))
            callback(acc, err)
            return
        }
        callback(acc, nil)
    }()
}
 
func (acc *Account) SyncRemove (can *Cancellable) error {
 
    var cancellable *C.GCancellable
    if can != nil {
        cancellable = can.cancellable
    }
    var gerror *C.GError
    C.goa_account_call_remove_sync(acc.account, cancellable, &gerror)
    if gerror != nil {
        err := ErrorFromNative(unsafe.Pointer(gerror))
        return err
    }
    return nil
}
 
// =============================== OAuthBased ==============================
 
type OAuthBasedAsyncGetAccessTokenCallback func(man *OAuthBased,
    access_token string, access_token_secret string, expiration uint32,
    err error)
 
type OAuthBased struct {
    auth_base *C.GoaOAuthBased
}
 
func (oauth *OAuthBased) GetAccessToken (can *Cancellable,
    callback OAuthBasedAsyncGetAccessTokenCallback) {
    go func() {
        var cancellable *C.GCancellable
        if can != nil {
            cancellable = can.cancellable
        }
        var gaccess_token *C.gchar
        var gaccess_token_secret *C.gchar
        var gexpiration C.gint
        var gerror *C.GError
 
        C.goa_oauth_based_call_get_access_token_sync(oauth.auth_base, &gaccess_token,
            &gaccess_token_secret, &gexpiration, cancellable, &gerror)
        if gerror != nil {
            err := ErrorFromNative(unsafe.Pointer(gerror))
            callback(oauth, "", "", 0, err)
            return
        }
        // get the go types
 
        access_token := C.GoString(C.to_charptr(gaccess_token))
        access_token_secret := C.GoString(C.to_charptr(gaccess_token_secret))
        callback(oauth, access_token, access_token_secret,
            uint32(gexpiration), nil)
    }()
}
 
func (oauth *OAuthBased) GetAccessTokenSync (
    can *Cancellable) (string, string, uint32, error){
 
    var cancellable *C.GCancellable
    if can != nil {
        cancellable = can.cancellable
    }
    var gaccess_token *C.gchar
    var gaccess_token_secret *C.gchar
    var gexpiration C.gint
    var gerror *C.GError
 
    C.goa_oauth_based_call_get_access_token_sync(oauth.auth_base, &gaccess_token,
        &gaccess_token_secret, &gexpiration, cancellable, &gerror)
    if gerror != nil {
        err := ErrorFromNative(unsafe.Pointer(gerror))
        return "", "", 0, err
    }
    // get the go types
 
    access_token := C.GoString(C.to_charptr(gaccess_token))
    access_token_secret := C.GoString(C.to_charptr(gaccess_token_secret))
    return access_token, access_token_secret, uint32(gexpiration), nil
}
 
func (oauth *OAuthBased) GetConsumerKey () string {
    consumer_key := C.goa_oauth_based_dup_consumer_key(oauth.auth_base)
    defer C.free(unsafe.Pointer(consumer_key))
    return C.GoString(C.to_charptr(consumer_key))
}
 
func (oauth *OAuthBased) GetConsumerSecret () string {
    consumer_secret := C.goa_oauth_based_dup_consumer_secret(oauth.auth_base)
    defer C.free(unsafe.Pointer(consumer_secret))
    return C.GoString(C.to_charptr(consumer_secret))
}
 
// =============================== Auth2Based ===============================
 
type OAuthBased2AsyncGetAccessTokenCallback func(man *OAuth2Based,
    access_token string, expiration uint32, err error)
 
type OAuth2Based struct {
    auth_base *C.GoaOAuth2Based
}
 
func (oauth *OAuth2Based) GetClientId () string {
    id := C.goa_oauth2_based_dup_client_id(oauth.auth_base)
    defer C.free(unsafe.Pointer(id))
    return C.GoString(C.to_charptr(id))
}
 
func (oauth *OAuth2Based) GetClientSecret () string {
    secret := C.goa_oauth2_based_dup_client_secret(oauth.auth_base)
    defer C.free(unsafe.Pointer(secret))
    return C.GoString(C.to_charptr(secret))
}
 
func (oauth *OAuth2Based) CallGetAccessToken (can *Cancellable,
    callback OAuthBased2AsyncGetAccessTokenCallback) {
 
    go func() {
        var cancellable *C.GCancellable
        if can != nil {
            cancellable = can.cancellable
        }
        var gaccess_token *C.gchar
        var gexpiration C.gint
        var gerror *C.GError
        C.goa_oauth2_based_call_get_access_token_sync(oauth.auth_base,
            &gaccess_token, &gexpiration, cancellable, &gerror)
        if gerror != nil {
            err := ErrorFromNative(unsafe.Pointer(gerror))
            callback(oauth, "", 0, err)
            return
        }
        access_token := C.GoString(C.to_charptr(gaccess_token))
        callback(oauth, access_token, uint32(gexpiration), nil)
    }()
}
 
func (oauth *OAuth2Based) CallGetAccessTokenSync (
    can *Cancellable) (string, uint32, error) {
 
    var cancellable *C.GCancellable
    if can != nil {
        cancellable = can.cancellable
    }
    var gaccess_token *C.gchar
    var gexpiration C.gint
    var gerror *C.GError
    C.goa_oauth2_based_call_get_access_token_sync(oauth.auth_base,
        &gaccess_token, &gexpiration, cancellable, &gerror)
    if gerror != nil {
        err := ErrorFromNative(unsafe.Pointer(gerror))
        return "", 0, err
     }
     access_token := C.GoString(C.to_charptr(gaccess_token))
     return access_token, uint32(gexpiration), nil
}
 
// =================================== Mail =================================
 
type Mail struct {
    mail *C.GoaMail
}
 
func (mail *Mail) GetEmailAdress () string {
    address := C.goa_mail_dup_email_address(mail.mail)
    defer C.free(unsafe.Pointer(address))
    return C.GoString(C.to_charptr(address))
}
 
func (mail *Mail) GetImapHost () string {
    host := C.goa_mail_dup_imap_host(mail.mail)
    defer C.free(unsafe.Pointer(host))
    return C.GoString(C.to_charptr(host))
}
 
func (mail *Mail) GetImapSupported () bool {
    return C.TRUE == C.goa_mail_get_imap_supported(mail.mail)
}
 
func (mail *Mail) GetImapUseTls () bool {
    return C.TRUE == C.goa_mail_get_imap_use_tls(mail.mail)
}
 
func (mail *Mail) GetImapUserName () string {
    username := C.goa_mail_dup_imap_user_name(mail.mail)
    defer C.free(unsafe.Pointer(username))
    return C.GoString(C.to_charptr(username))
}
 
func (mail *Mail) GetSmtpHost () string {
    host := C.goa_mail_dup_smtp_host(mail.mail)
    defer C.free(unsafe.Pointer(host))
    return C.GoString(C.to_charptr(host))
}
 
func (mail *Mail) GetSmtpSupported () bool {
    return C.TRUE == C.goa_mail_get_smtp_supported(mail.mail)
}
 
func (mail *Mail) GetSmtpUseTls () bool {
    return C.TRUE == C.goa_mail_get_smtp_use_tls(mail.mail)
}
 
func (mail *Mail) GetSmtpUserName () string {
    username := C.goa_mail_dup_smtp_user_name(mail.mail)
    defer C.free(unsafe.Pointer(username))
    return C.GoString(C.to_charptr(username))
}
 
// ================================== Calendar ==============================
 
type Calendar struct {
    calendar *C.GoaCalendar
}
 
// =================================== Contacts =============================
 
type Contacts struct {
    contacts *C.GoaContacts
}
 
// ===================================== Chat ===============================
 
type Chat struct {
    chat *C.GoaChat
}
 
// ===================================== Documents ===========================
 
type Documents struct {
    docs *C.GoaDocuments
}
 
// ====================================== Exchanges ==========================
 
type Exchange struct {
    exchange *C.GoaExchange
}
 
// ================================== Object ===============================
 
type Object struct {
    object *C.GoaObject
}
 
func (obj *Object) GetManager () *Manager {
    c_data := C.goa_object_get_manager(obj.object)
    if c_data != nil {
        return &Manager{manager:c_data}
    }
    return nil
}
 
func (obj *Object) GetAccount () *Account {
    c_data := C.goa_object_get_account(obj.object)
    if c_data != nil {
        return &Account{account:c_data}
    }
    return nil
}
 
func (obj *Object) GetOauthBased () *OAuthBased {
    c_data := C.goa_object_get_oauth_based(obj.object)
    if c_data != nil {
        return &OAuthBased{auth_base:c_data}
    }
    return nil
}
 
func (obj *Object) GetOauth2Based () *OAuth2Based {
    c_data := C.goa_object_get_oauth2_based(obj.object)
    if c_data != nil {
        return &OAuth2Based{auth_base:c_data}
    }
    return nil
}
 
func (obj *Object) GetMail () *Mail {
    c_data := C.goa_object_get_mail(obj.object)
    if c_data != nil {
        return &Mail{mail:c_data}
    }
    return nil
}
 
func (obj *Object) GetCalendar () *Calendar {
    c_data := C.goa_object_get_calendar(obj.object)
    if c_data != nil {
        return &Calendar{calendar:c_data}
    }
    return nil
}
 
func (obj *Object) GetContacts () *Contacts {
    c_data := C.goa_object_get_contacts(obj.object)
    if c_data != nil {
        return &Contacts{contacts:c_data}
    }
    return nil
}
 
func (obj *Object) GetChat () *Chat {
    c_data := C.goa_object_get_chat(obj.object)
    if c_data != nil {
        return &Chat{chat:c_data}
    }
    return nil
}
 
func (obj *Object) GetDocuments () *Documents {
    c_data := C.goa_object_get_documents(obj.object)
    if c_data != nil {
        return &Documents{docs:c_data}
    }
    return nil
}
 
func (obj *Object) GetExchange () *Exchange {
    c_data := C.goa_object_get_exchange(obj.object)
    if c_data != nil {
        return &Exchange{exchange:c_data}
    }
    return nil
}
 
// ================================== Client ===============================
 
type ClientAsyncReadyCallback func(client *Client, err error)
 
type Client struct {
    client *C.GoaClient
}
 
func NewSyncClient (can *Cancellable) (*Client, error) {
    var gerror *C.GError
    var cancellable *C.GCancellable
    if can != nil {
        cancellable = can.cancellable
    }
    goa_client := &Client{client:C.goa_client_new_sync(cancellable, &gerror)}
    if gerror != nil {
        err := ErrorFromNative(unsafe.Pointer(gerror))
		return nil, err
    }
    return goa_client, nil
}
 
func NewClient (can *Cancellable, callback ClientAsyncReadyCallback) {
    go func(){
        var gerror *C.GError
        var cancellable *C.GCancellable
        if can != nil {
            cancellable = can.cancellable
        }
        goa_client := &Client{
            client:C.goa_client_new_sync(cancellable, &gerror)}
 
        if gerror != nil {
            err := ErrorFromNative(unsafe.Pointer(gerror))
		    callback(nil, err)
            return
        }
        callback(goa_client, nil)
    }()
}
 
func (client *Client) GetManager () *Manager {
    return &Manager{manager:C.goa_client_get_manager(client.client)}
}
 
func (client *Client) GetAccounts () ([]*Object, uint) {
    accounts_list := C.goa_client_get_accounts(client.client)
    // free the glist
    defer C.g_list_free(accounts_list)
 
    length := C.g_list_length(accounts_list)
    result := make([]*Object, C.g_list_length(accounts_list))
    // construct each of the goa objects and add them to the array
 
    for n := C.guint(0); n < length; n++ {
        data:= C.g_list_nth_data(accounts_list, n)
        pointer := unsafe.Pointer(data)
        goa_object := &Object{object:C.to_GoaObject(pointer)}
        result[uint(n)] = goa_object
    }
 
    return result, uint(length)
}
 
func (client *Client) LookupById (account_id string) *Object {
    account_id_str := C.CString(account_id)
    defer C.free_string(account_id_str)
 
    return &Object{object:C.goa_client_lookup_by_id(client.client,
        C.to_gcharptr(account_id_str))}
}
 
func (client *Client) Connect(s string, f interface{}, datas ...interface{}) {
    glib.ObjectFromNative(unsafe.Pointer(
        C.to_GObject(unsafe.Pointer(client.client)))).Connect(s, f, datas...)
}
 
 
func main() {
    fmt.Println("test")
    gtk.Init(&os.Args)
    go func () {
        client, err := NewSyncClient(nil)
        if err != nil {
            fmt.Println("Got error getting client")
        }
        accounts, length := client.GetAccounts()
        fmt.Printf("Got %d accounts backn", length)
        for n := uint(0); n < length; n++ {
            account := accounts[n].GetAccount()
            fmt.Printf("Account %sn", account.GetId())
            fmt.Println("tAccount info:")
            fmt.Printf("ttProvider type:tt%sn", account.GetProviderType())
            fmt.Printf("ttProvider name:tt%sn", account.GetProviderName())
            fmt.Printf("ttIdentity:tt%sn", account.GetIdentity())
            fmt.Printf("ttPresentation identity:t%sn",
                account.GetPresentationIdentity())
            mail := accounts[n].GetMail()
            if mail != nil {
                fmt.Printf("tEmail info:n")
                fmt.Printf("ttEmail address:tt%sn", mail.GetEmailAdress())
                fmt.Printf("ttImap host:tt%sn", mail.GetImapHost())
                fmt.Printf("ttSmtp host:tt%sn", mail.GetSmtpHost())
            }
        }
    }()
    gtk.Main()
}

Read more
mandel

I really hate my voice when I hear myself in videos etc.. but well, it happens to most of us. Here is the small rant ralsina and I had about multi-platform programming with python:

Read more
mandel

The following behaviour completely got me out of place:

>>> byte_string = b'Test'
>>> byte_string[0] == b'T'
False
>>> byte_string[0] in b'T'
True
>>> byte_string[0]        
84
>>> b'T'
b'T'

Turns out this is documented (blame me I did not read the docs!!!). The correct way to do it is:

>>> byte_string = b'Test'
>>> byte_string[:1] == b'T'
True

I really hope we don’t have any code that does this operations.

Read more
mandel

So yet again I have been confronted with broken tests in Ubuntu One. As I have already mentioned before I have spent a significant amount of time ensuring that the tests of Ubuntu One (which use twisted a lot) are deterministic and we do not leave a dirty reactor in the way. In order to do that a few week a go I wrote the following code that will help the rest of the team write such tests:

import os
import shutil
import tempfile
 
from twisted.internet import defer, endpoints, protocol
from twisted.spread import pb
 
from ubuntuone.devtools.testcases import BaseTestCase
 
# no init method +  twisted common warnings
# pylint: disable=W0232, C0103, E1101
 
 
def server_protocol_factory(cls):
    """Factory to create tidy protocols."""
 
    if cls is None:
        cls = protocol.Protocol
 
    class ServerTidyProtocol(cls):
        """A tidy protocol."""
 
        def connectionLost(self, *args):
            """Lost the connection."""
            cls.connectionLost(self, *args)
            # lets tell everyone
            # pylint: disable=W0212
            if (self.factory._disconnecting
                    and self.factory.testserver_on_connection_lost is not None
                    and not self.factory.testserver_on_connection_lost.called):
                self.factory.testserver_on_connection_lost.callback(self)
            # pylint: enable=W0212
 
    return ServerTidyProtocol
 
 
def client_protocol_factory(cls):
    """Factory to create tidy protocols."""
 
    if cls is None:
        cls = protocol.Protocol
 
    class ClientTidyProtocol(cls):
        """A tidy protocol."""
 
        def connectionLost(self, *a):
            """Connection list."""
            # pylint: disable=W0212
            if (self.factory._disconnecting
                    and self.factory.testserver_on_connection_lost is not None
                    and not self.factory.testserver_on_connection_lost.called):
                self.factory.testserver_on_connection_lost.callback(self)
            # pylint: enable=W0212
            cls.connectionLost(self, *a)
 
    return ClientTidyProtocol
 
 
class TidySocketServer(object):
    """Ensure that twisted servers are correctly managed in tests.
 
    Closing a twisted server is a complicated matter. In order to do so you
    have to ensure that three different deferreds are fired:
 
        1. The server must stop listening.
        2. The client connection must disconnect.
        3. The server connection must disconnect.
 
    This class allows to create a server and a client that will ensure that
    the reactor is left clean by following the pattern described at
    http://mumak.net/stuff/twisted-disconnect.html
    """
    def __init__(self):
        """Create a new instance."""
        self.listener = None
        self.server_factory = None
 
        self.connector = None
        self.client_factory = None
 
    def get_server_endpoint(self):
        """Return the server endpoint description."""
        raise NotImplementedError('To be implemented by child classes.')
 
    def get_client_endpoint(self):
        """Return the client endpoint description."""
        raise NotImplementedError('To be implemented by child classes.')
 
    @defer.inlineCallbacks
    def listen_server(self, server_class, *args, **kwargs):
        """Start a server in a random port."""
        from twisted.internet import reactor
        self.server_factory = server_class(*args, **kwargs)
        self.server_factory._disconnecting = False
        self.server_factory.testserver_on_connection_lost = defer.Deferred()
        self.server_factory.protocol = server_protocol_factory(
                                                 self.server_factory.protocol)
        endpoint = endpoints.serverFromString(reactor,
                                              self.get_server_endpoint())
        self.listener = yield endpoint.listen(self.server_factory)
        defer.returnValue(self.server_factory)
 
    @defer.inlineCallbacks
    def connect_client(self, client_class, *args, **kwargs):
        """Conect a client to a given server."""
        from twisted.internet import reactor
 
        if self.server_factory is None:
            raise ValueError('Server Factory was not provided.')
        if self.listener is None:
            raise ValueError('%s has not started listening.',
                             self.server_factory)
 
        self.client_factory = client_class(*args, **kwargs)
        self.client_factory._disconnecting = False
        self.client_factory.protocol = client_protocol_factory(
                                                 self.client_factory.protocol)
        self.client_factory.testserver_on_connection_lost = defer.Deferred()
        endpoint = endpoints.clientFromString(reactor,
                                                    self.get_client_endpoint())
        self.connector = yield endpoint.connect(self.client_factory)
        defer.returnValue(self.client_factory)
 
    def clean_up(self):
        """Action to be performed for clean up."""
        if self.server_factory is None or self.listener is None:
            # nothing to clean
            return defer.succeed(None)
 
        if self.listener and self.connector:
            # clean client and server
            self.server_factory._disconnecting = True
            self.client_factory._disconnecting = True
            self.connector.transport.loseConnection()
            d = defer.maybeDeferred(self.listener.stopListening)
            return defer.gatherResults([d,
                self.client_factory.testserver_on_connection_lost,
                self.server_factory.testserver_on_connection_lost])
        if self.listener:
            # just clean the server since there is no client
            self.server_factory._disconnecting = True
            return defer.maybeDeferred(self.listener.stopListening)
 
 
class TidyTCPServer(TidySocketServer):
    """A tidy tcp domain sockets server."""
 
    client_endpoint_pattern = 'tcp:host=127.0.0.1:port=%s'
    server_endpoint_pattern = 'tcp:0:interface=127.0.0.1'
 
    def get_server_endpoint(self):
        """Return the server endpoint description."""
        return self.server_endpoint_pattern
 
    def get_client_endpoint(self):
        """Return the client endpoint description."""
        if self.server_factory is None:
            raise ValueError('Server Factory was not provided.')
        if self.listener is None:
            raise ValueError('%s has not started listening.',
                                                          self.server_factory)
        return self.client_endpoint_pattern % self.listener.getHost().port
 
 
class TidyUnixServer(TidySocketServer):
    """A tidy unix domain sockets server."""
 
    client_endpoint_pattern = 'unix:path=%s'
    server_endpoint_pattern = 'unix:%s'
 
    def __init__(self):
        """Create a new instance."""
        super(TidyUnixServer, self).__init__()
        self.temp_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.temp_dir, 'tidy_unix_server')
 
    def get_server_endpoint(self):
        """Return the server endpoint description."""
        return self.server_endpoint_pattern % self.path
 
    def get_client_endpoint(self):
        """Return the client endpoint description."""
        return self.client_endpoint_pattern % self.path
 
    def clean_up(self):
        """Action to be performed for clean up."""
        result = super(TidyUnixServer, self).clean_up()
        # remove the dir once we are disconnected
        result.addCallback(lambda _: shutil.rmtree(self.temp_dir))
        return result
 
 
class ServerTestCase(BaseTestCase):
    """Base test case for tidy servers."""
 
    @defer.inlineCallbacks
    def setUp(self):
        """Set the diff tests."""
        yield super(ServerTestCase, self).setUp()
 
        try:
            self.server_runner = self.get_server()
        except NotImplementedError:
            self.server_runner = None
 
        self.server_factory = None
        self.client_factory = None
        self.server_disconnected = None
        self.client_connected = None
        self.client_disconnected = None
        self.listener = None
        self.connector = None
        self.addCleanup(self.tear_down_server_client)
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        raise NotImplementedError('To be implemented by child classes.')
 
    @defer.inlineCallbacks
    def listen_server(self, server_class, *args, **kwargs):
        """Listen a server.
 
        The method takes the server class and the arguments that should be
        passed to the server constructor.
        """
        self.server_factory = yield self.server_runner.listen_server(
                                                server_class, *args, **kwargs)
        self.server_disconnected = 
                self.server_factory.testserver_on_connection_lost
        self.listener = self.server_runner.listener
 
    @defer.inlineCallbacks
    def connect_client(self, client_class, *args, **kwargs):
        """Connect the client.
 
        The method takes the client factory  class and the arguments that
        should be passed to the client constructor.
        """
        self.client_factory = yield self.server_runner.connect_client(
                                                client_class, *args, **kwargs)
        self.client_disconnected = 
                self.client_factory.testserver_on_connection_lost
        self.connector = self.server_runner.connector
 
    def tear_down_server_client(self):
        """Clean the server and client."""
        if self.server_runner:
            return self.server_runner.clean_up()
 
 
class TCPServerTestCase(ServerTestCase):
    """Test that uses a single twisted server."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyTCPServer()
 
 
class UnixServerTestCase(ServerTestCase):
    """Test that uses a single twisted server."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyUnixServer()
 
 
class PbServerTestCase(ServerTestCase):
    """Test a pb server."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        raise NotImplementedError('To be implemented by child classes.')
 
    @defer.inlineCallbacks
    def listen_server(self, *args, **kwargs):
        """Listen a pb server."""
        yield super(PbServerTestCase, self).listen_server(pb.PBServerFactory,
                                                              *args, **kwargs)
 
    @defer.inlineCallbacks
    def connect_client(self, *args, **kwargs):
        """Connect a pb client."""
        yield super(PbServerTestCase, self).connect_client(pb.PBClientFactory,
                                                              *args, **kwargs)
 
 
class TCPPbServerTestCase(PbServerTestCase):
    """Test a pb server over TCP."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyTCPServer()
 
 
class UnixPbServerTestCase(PbServerTestCase):
    """Test a pb server over Unix domain sockets."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyUnixServer()

The idea of the code is that developers do not need to worry about how to stop listening ports in their tests and just write tests like the following:

class TCPMultipleServersTestCase(TestCase):
    """Ensure that several servers can be ran."""
 
    timeout = 2
 
    @defer.inlineCallbacks
    def setUp(self):
        """Set the diff tests."""
        yield super(TCPMultipleServersTestCase, self).setUp()
        self.first_tcp_server = self.get_server()
        self.second_tcp_server = self.get_server()
        self.adder = Adder()
        self.calculator = Calculator(self.adder)
        self.echoer = Echoer()
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyTCPServer()
 
    @defer.inlineCallbacks
    def test_single_server(self):
        """Test setting a single server."""
        first_number = 1
        second_number = 2
        yield self.first_tcp_server.listen_server(pb.PBServerFactory,
                                                              self.calculator)
        self.addCleanup(self.first_tcp_server.clean_up)
        calculator_c = yield self.first_tcp_server.connect_client(
                                                           pb.PBClientFactory)
        calculator = yield calculator_c.getRootObject()
        adder = yield calculator.callRemote('get_adder')
        result = yield adder.callRemote('add', first_number, second_number)
        self.assertEqual(first_number + second_number, result)
 
    @defer.inlineCallbacks
    def test_multiple_server(self):
        """Test setting multiple server."""
        first_number = 1
        second_number = 2
        # first server
        yield self.first_tcp_server.listen_server(pb.PBServerFactory,
                                                              self.calculator)
        self.addCleanup(self.first_tcp_server.clean_up)
 
        # second server
        yield self.second_tcp_server.listen_server(pb.PBServerFactory,
                                                   self.echoer)
        self.addCleanup(self.second_tcp_server.clean_up)
 
        # connect the diff clients
        calculator_c = yield self.first_tcp_server.connect_client(
                                                           pb.PBClientFactory)
        echoer_c = yield self.second_tcp_server.connect_client(
                                                           pb.PBClientFactory)
 
        calculator = yield calculator_c.getRootObject()
        adder = yield calculator.callRemote('get_adder')
        result = yield adder.callRemote('add', first_number, second_number)
        self.assertEqual(first_number + second_number, result)
        echoer = yield echoer_c.getRootObject()
        echo = yield echoer.callRemote('say', 'hello')
        self.assertEqual(self.echoer.remote_say('hello'), echo)

As you can see those tests do not give a rats ass about ensuring that the clients lose connection or we stop listening ports… Or so I though because the following code made such approach break in Mac OS X (although I suspect it was broken on Linux and Windows but we never experienced it):

class NullProtocol(protocol.Protocol):
    """A protocol that drops the connection."""
 
    def connectionMade(self):
        """Just drop the connection."""
        self.transport.loseConnection()
 
 
class PortDetectFactory(protocol.ClientFactory):
    """Will detect if something is listening in a given port."""
 
    protocol = NullProtocol
 
    def __init__(self):
        """Initialize this instance."""
        self.d = defer.Deferred()
 
    def is_listening(self):
        """A deferred that will become True if something is listening."""
        return self.d
 
    def buildProtocol(self, addr):
        """Connected."""
        p = protocol.ClientFactory.buildProtocol(self, addr)
        if not self.d.called:
            self.d.callback(True)
        return p
 
    def clientConnectionLost(self, connector, reason):
        """The connection was lost."""
        if not self.d.called:
            self.d.callback(False)
 
    def clientConnectionFailed(self, connector, reason):
        """The connection failed."""
        if not self.d.called:
            self.d.callback(False)

The code used to test the above was written as:

    @defer.inlineCallbacks
    def test_is_already_running(self):
        """The is_already_running method returns True if already started."""
        server = self.get_server()
        self.addCleanup(server.clean_up)
 
        class TestConnect(object):
 
            @defer.inlineCallbacks
            def connect(my_self, factory):
                connected_factory = yield server.connect_client(PortDetectFactory)
                self.patch(factory, 'is_listening', lambda:
                        connected_factory.is_listening())
                defer.returnValue(connected_factory)
 
        self.patch(tcpactivation, 'clientFromString', lambda *args: TestConnect())
 
        yield server.listen_server(protocol.ServerFactory)
 
        # pylint: disable=E1101
        ad = ActivationDetector(self.config)
        result = yield ad.is_already_running()
        self.assertTrue(result, "It should be already running.")

While in all the other platforms the tests passed with no problems on Mac OS X the tests would block in the clean_up method from the server because the deferred that was called in the connectionLost from the ServerTidyProtocol was never fired… Interesting.. After digging in the code I realized that the main issue with the approach of the clean_up code was wrong. The problem relies on the way in which the NullProtocol works. As you can see in the code the protocol loses its connections as soon as it made. This results in to possible things:

  1. The server does know that we have a client connected and calls buildProtocol.
  2. The connection is lost so fast that the buildProtocol on the ServerFactory does not get call.

When running the tests on Windows and Linux we were always facing the first scenario, buildProtocol was called which meant that connectionLost in the server protocol would be called. On the other hand, on Mac OS X, 1 out of 10 runs of the tests would block in the clean up because we would be in the second scenario, that is, no protocol would be build in the ServerFactory which results in the connectionLost never being called because it was no needed. The work around this issue is quite simple once you understand what is going on. The ServerFactory has to be modified to set the deferred when buildProtocol is called and not before ensuring that when we cleanup we check if the deferred is None and if it is not we wait for it to be fired. The fixed version of the helper code is the following:

import os
import shutil
import tempfile
 
from twisted.internet import defer, endpoints, protocol
from twisted.spread import pb
 
from ubuntuone.devtools.testcases import BaseTestCase
 
# no init method + twisted common warnings
# pylint: disable=W0232, C0103, E1101
 
 
def server_protocol_factory(cls):
    """Factory to create tidy protocols."""
 
    if cls is None:
        cls = protocol.Protocol
 
    class ServerTidyProtocol(cls):
        """A tidy protocol."""
 
        def connectionLost(self, *args):
            """Lost the connection."""
            cls.connectionLost(self, *args)
            # lets tell everyone
            # pylint: disable=W0212
            if (self.factory._disconnecting
                    and self.factory.testserver_on_connection_lost is not None
                    and not self.factory.testserver_on_connection_lost.called):
                self.factory.testserver_on_connection_lost.callback(self)
            # pylint: enable=W0212
 
    return ServerTidyProtocol
 
 
def server_factory_factory(cls):
    """Factory that creates special types of factories for tests."""
 
    if cls is None:
        cls = protocol.ServerFactory
 
    class TidyServerFactory(cls):
        """A tidy factory."""
 
        testserver_on_connection_lost = None
 
        def buildProtocol(self, addr):
            prot = cls.buildProtocol(self, addr)
            self.testserver_on_connection_lost = defer.Deferred()
            return prot
 
    return TidyServerFactory
 
 
def client_protocol_factory(cls):
    """Factory to create tidy protocols."""
 
    if cls is None:
        cls = protocol.Protocol
 
    class ClientTidyProtocol(cls):
        """A tidy protocol."""
 
        def connectionLost(self, *a):
            """Connection list."""
            cls.connectionLost(self, *a)
            # pylint: disable=W0212
            if (self.factory._disconnecting
                    and self.factory.testserver_on_connection_lost is not None
                    and not self.factory.testserver_on_connection_lost.called):
                self.factory.testserver_on_connection_lost.callback(self)
            # pylint: enable=W0212
 
    return ClientTidyProtocol
 
 
class TidySocketServer(object):
    """Ensure that twisted servers are correctly managed in tests.
 
    Closing a twisted server is a complicated matter. In order to do so you
    have to ensure that three different deferreds are fired:
 
        1. The server must stop listening.
        2. The client connection must disconnect.
        3. The server connection must disconnect.
 
    This class allows to create a server and a client that will ensure that
    the reactor is left clean by following the pattern described at
    http://mumak.net/stuff/twisted-disconnect.html
    """
    def __init__(self):
        """Create a new instance."""
        self.listener = None
        self.server_factory = None
 
        self.connector = None
        self.client_factory = None
 
    def get_server_endpoint(self):
        """Return the server endpoint description."""
        raise NotImplementedError('To be implemented by child classes.')
 
    def get_client_endpoint(self):
        """Return the client endpoint description."""
        raise NotImplementedError('To be implemented by child classes.')
 
    @defer.inlineCallbacks
    def listen_server(self, server_class, *args, **kwargs):
        """Start a server in a random port."""
        from twisted.internet import reactor
        tidy_class = server_factory_factory(server_class)
        self.server_factory = tidy_class(*args, **kwargs)
        self.server_factory._disconnecting = False
        self.server_factory.protocol = server_protocol_factory(
                                                 self.server_factory.protocol)
        endpoint = endpoints.serverFromString(reactor,
                                              self.get_server_endpoint())
        self.listener = yield endpoint.listen(self.server_factory)
        defer.returnValue(self.server_factory)
 
    @defer.inlineCallbacks
    def connect_client(self, client_class, *args, **kwargs):
        """Conect a client to a given server."""
        from twisted.internet import reactor
 
        if self.server_factory is None:
            raise ValueError('Server Factory was not provided.')
        if self.listener is None:
            raise ValueError('%s has not started listening.',
                             self.server_factory)
 
        self.client_factory = client_class(*args, **kwargs)
        self.client_factory._disconnecting = False
        self.client_factory.protocol = client_protocol_factory(
                                                 self.client_factory.protocol)
        self.client_factory.testserver_on_connection_lost = defer.Deferred()
        endpoint = endpoints.clientFromString(reactor,
                                                    self.get_client_endpoint())
        self.connector = yield endpoint.connect(self.client_factory)
        defer.returnValue(self.client_factory)
 
    def clean_up(self):
        """Action to be performed for clean up."""
        if self.server_factory is None or self.listener is None:
            # nothing to clean
            return defer.succeed(None)
 
        if self.listener and self.connector:
            # clean client and server
            self.server_factory._disconnecting = True
            self.client_factory._disconnecting = True
            d = defer.maybeDeferred(self.listener.stopListening)
            self.connector.transport.loseConnection()
            if self.server_factory.testserver_on_connection_lost:
                return defer.gatherResults([d,
                    self.client_factory.testserver_on_connection_lost,
                    self.server_factory.testserver_on_connection_lost])
            else:
                return defer.gatherResults([d,
                    self.client_factory.testserver_on_connection_lost])
        if self.listener:
            # just clean the server since there is no client
            # pylint: disable=W0201
            self.server_factory._disconnecting = True
            return defer.maybeDeferred(self.listener.stopListening)
            # pylint: enable=W0201
 
 
class TidyTCPServer(TidySocketServer):
    """A tidy tcp domain sockets server."""
 
    client_endpoint_pattern = 'tcp:host=127.0.0.1:port=%s'
    server_endpoint_pattern = 'tcp:0:interface=127.0.0.1'
 
    def get_server_endpoint(self):
        """Return the server endpoint description."""
        return self.server_endpoint_pattern
 
    def get_client_endpoint(self):
        """Return the client endpoint description."""
        if self.server_factory is None:
            raise ValueError('Server Factory was not provided.')
        if self.listener is None:
            raise ValueError('%s has not started listening.',
                                                          self.server_factory)
        return self.client_endpoint_pattern % self.listener.getHost().port
 
 
class TidyUnixServer(TidySocketServer):
    """A tidy unix domain sockets server."""
 
    client_endpoint_pattern = 'unix:path=%s'
    server_endpoint_pattern = 'unix:%s'
 
    def __init__(self):
        """Create a new instance."""
        super(TidyUnixServer, self).__init__()
        self.temp_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.temp_dir, 'tidy_unix_server')
 
    def get_server_endpoint(self):
        """Return the server endpoint description."""
        return self.server_endpoint_pattern % self.path
 
    def get_client_endpoint(self):
        """Return the client endpoint description."""
        return self.client_endpoint_pattern % self.path
 
    def clean_up(self):
        """Action to be performed for clean up."""
        result = super(TidyUnixServer, self).clean_up()
        # remove the dir once we are disconnected
        result.addCallback(lambda _: shutil.rmtree(self.temp_dir))
        return result
 
 
class ServerTestCase(BaseTestCase):
    """Base test case for tidy servers."""
 
    @defer.inlineCallbacks
    def setUp(self):
        """Set the diff tests."""
        yield super(ServerTestCase, self).setUp()
 
        try:
            self.server_runner = self.get_server()
        except NotImplementedError:
            self.server_runner = None
 
        self.server_factory = None
        self.client_factory = None
        self.server_disconnected = None
        self.client_connected = None
        self.client_disconnected = None
        self.listener = None
        self.connector = None
        self.addCleanup(self.tear_down_server_client)
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        raise NotImplementedError('To be implemented by child classes.')
 
    @defer.inlineCallbacks
    def listen_server(self, server_class, *args, **kwargs):
        """Listen a server.
 
        The method takes the server class and the arguments that should be
        passed to the server constructor.
        """
        self.server_factory = yield self.server_runner.listen_server(
                                                server_class, *args, **kwargs)
        self.server_disconnected = 
                self.server_factory.testserver_on_connection_lost
        self.listener = self.server_runner.listener
 
    @defer.inlineCallbacks
    def connect_client(self, client_class, *args, **kwargs):
        """Connect the client.
 
        The method takes the client factory  class and the arguments that
        should be passed to the client constructor.
        """
        self.client_factory = yield self.server_runner.connect_client(
                                                client_class, *args, **kwargs)
        self.client_disconnected = 
                self.client_factory.testserver_on_connection_lost
        self.connector = self.server_runner.connector
 
    def tear_down_server_client(self):
        """Clean the server and client."""
        if self.server_runner:
            return self.server_runner.clean_up()
 
 
class TCPServerTestCase(ServerTestCase):
    """Test that uses a single twisted server."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyTCPServer()
 
 
class UnixServerTestCase(ServerTestCase):
    """Test that uses a single twisted server."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyUnixServer()
 
 
class PbServerTestCase(ServerTestCase):
    """Test a pb server."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        raise NotImplementedError('To be implemented by child classes.')
 
    @defer.inlineCallbacks
    def listen_server(self, *args, **kwargs):
        """Listen a pb server."""
        yield super(PbServerTestCase, self).listen_server(pb.PBServerFactory,
                                                              *args, **kwargs)
 
    @defer.inlineCallbacks
    def connect_client(self, *args, **kwargs):
        """Connect a pb client."""
        yield super(PbServerTestCase, self).connect_client(pb.PBClientFactory,
                                                              *args, **kwargs)
 
 
class TCPPbServerTestCase(PbServerTestCase):
    """Test a pb server over TCP."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyTCPServer()
 
 
class UnixPbServerTestCase(PbServerTestCase):
    """Test a pb server over Unix domain sockets."""
 
    def get_server(self):
        """Return the server to be used to run the tests."""
        return TidyUnixServer()

I wonder if at some point I should share this code for the people out there… any opinions?

Read more
mandel

The last few days I have taken a considerable time to remove all the errors from the Ubuntu One tests on Windows. The tests were leaving tcp connections in the twisted reactor that on some Windows systems will result on a DirtyReactorError which is a PITA. Due to a refactor the pattern I described here was remove (ouch!) and I had to re-add it. In this case, instead of doing the pattern everywhere is needed I created some helper classes that will ensure that the clients and servers are correctly cleaned up after you use them.

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from twisted.internet import defer, protocol, reactor
from twisted.spread import pb
 
from ubuntuone.devtools.testcases import BaseTestCase
 
# no init method +  twisted common warnings
# pylint: disable=W0232, C0103, E1101
 
 
def server_protocol_factory(cls):
    """Factory to create tidy protocols."""
 
    if cls is None:
        cls = protocol.Protocol
 
    class ServerSaveProtocol(cls):
        """A tidy protocol."""
 
        def connectionLost(self, *args):
            """Lost the connection."""
            cls.connectionLost(self, *args)
            # lets tell everyone
            # pylint: disable=W0212
            if (self.factory._disconnecting
                    and self.factory.testserver_on_connection_lost is not None
                    and not self.factory.testserver_on_connection_lost.called):
                self.factory.testserver_on_connection_lost.callback(self)
            # pylint: enable=W0212
 
    return ServerSaveProtocol
 
 
def client_protocol_factory(cls):
    """Factory to create tidy protocols."""
 
    if cls is None:
        cls = protocol.Protocol
 
    class ClientSaveProtocol(cls):
        """A tidy protocol."""
 
        def connectionMade(self):
            """Connection made."""
            if (self.factory.testserver_on_connection_made is not None
                    and not self.factory.testserver_on_connection_made.called):
                self.factory.testserver_on_connection_made.callback(self)
            cls.connectionMade(self)
 
        def connectionLost(self, *a):
            """Connection list."""
            # pylint: disable=W0212
            if (self.factory._disconnecting
                    and self.factory.testserver_on_connection_lost is not None
                    and not self.factory.testserver_on_connection_lost.called):
                self.factory.testserver_on_connection_lost.callback(self)
            # pylint: enable=W0212
            cls.connectionLost(self, *a)
 
    return ClientSaveProtocol
 
 
class TidyTCPServer(object):
    """Ensure that twisted services are correctly managed in tests.
 
    Closing a twisted service is a complicated matter. In order to do so you
    have to ensure that three different deferreds are fired:
 
        1. The server must stop listening.
        2. The client connection must disconnect.
        3. The server connection must disconnect.
 
    This class allows to create a service and a client that will ensure that
    the reactor is left clean by following the pattern described at
    http://mumak.net/stuff/twisted-disconnect.html
    """
 
    def __init__(self):
        """Create a new instance."""
        self.listener = None
        self.server_factory = None
 
        self.connector = None
        self.client_factory = None
 
    def listen_server(self, server_class, *args, **kwargs):
        """Start a server in a random port."""
        # pylint: disable=W0621
        from twisted.internet import reactor
        # pylint: enable=W0621
        self.server_factory = server_class(*args, **kwargs)
        self.server_factory._disconnecting = False
        self.server_factory.testserver_on_connection_lost = defer.Deferred()
        self.server_factory.protocol = server_protocol_factory(
                                                 self.server_factory.protocol)
        self.listener = reactor.listenTCP(0, self.server_factory)
        return self.server_factory
 
    def connect_client(self, client_class, *args, **kwargs):
        """Conect a client to a given service."""
 
        if self.server_factory is None:
            raise ValueError('Server Factory was not provided.')
        if self.listener is None:
            raise ValueError('%s has not started listening.',
                                                          self.server_factory)
 
        self.client_factory = client_class(*args, **kwargs)
        self.client_factory._disconnecting = False
        self.client_factory.protocol = client_protocol_factory(
                                                 self.client_factory.protocol)
        self.client_factory.testserver_on_connection_made = defer.Deferred()
        self.client_factory.testserver_on_connection_lost = defer.Deferred()
        self.connector = reactor.connectTCP('localhost',
                                        self.listener.getHost().port,
                                        self.client_factory)
        return self.client_factory
 
    def clean_up(self):
        """Action to be performed for clean up."""
        if self.server_factory is None or self.listener is None:
            # nothing to clean
            return defer.succeed(None)
 
        if self.listener and self.connector:
            # clean client and server
            self.server_factory._disconnecting = True
            self.client_factory._disconnecting = True
            d = defer.maybeDeferred(self.listener.stopListening)
            self.connector.disconnect()
            return defer.gatherResults([d,
                self.client_factory.testserver_on_connection_lost,
                self.server_factory.testserver_on_connection_lost])
        if self.listener:
            # just clean the server since there is no client
            self.server_factory._disconnecting = True
            return defer.maybeDeferred(self.listener.stopListening)
 
 
class TCPServerTestCase(BaseTestCase):
    """Test that uses a single twisted service."""
 
    @defer.inlineCallbacks
    def setUp(self):
        """Set the diff tests."""
        yield super(TCPServerTestCase, self).setUp()
        self.service_runner = TidyTCPServer()
 
        self.server_factory = None
        self.client_factory = None
        self.server_disconnected = None
        self.client_connected = None
        self.client_disconnected = None
        self.listener = None
        self.connector = None
        self.addCleanup(self.tear_down_server_client)
 
    def listen_server(self, server_class, *args, **kwargs):
        """Listen a server.
 
        The method takes the server class and the arguments that should be
        passed to the server constructor.
        """
        self.server_factory = self.service_runner.listen_server(server_class,
                *args, **kwargs)
        self.server_disconnected = 
                self.server_factory.testserver_on_connection_lost
        self.listener = self.service_runner.listener
 
    def connect_client(self, client_class, *args, **kwargs):
        """Connect the client.
 
        The method takes the client factory  class and the arguments that
        should be passed to the client constructor.
        """
        self.client_factory = self.service_runner.connect_client(client_class,
                                                              *args, **kwargs)
        self.client_connected = 
                self.client_factory.testserver_on_connection_made
        self.client_disconnected = 
                self.client_factory.testserver_on_connection_lost
        self.connector = self.service_runner.connector
 
    def tear_down_server_client(self):
        """Clean the server and client."""
        if self.server_factory and self.client_factory:
            return self.service_runner.clean_up()
 
 
class PbServiceTestCase(TCPServerTestCase):
    """Test a pb service."""
 
    def listen_server(self, *args, **kwargs):
        """Listen a pb server."""
        super(PbServiceTestCase, self).listen_server(pb.PBServerFactory,
                                                              *args, **kwargs)
 
    def connect_client(self, *args, **kwargs):
        """Connect a pb client."""
        super(PbServiceTestCase, self).connect_client(pb.PBClientFactory,
                                                              *args, **kwargs)

The above classes can be used in the following way:

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
from twisted.internet import defer, protocol
from twisted.spread import pb
from twisted.trial.unittest import TestCase
 
from ubuntuone.devtools.testcases.txtcpserver import (
    client_protocol_factory,
    server_protocol_factory,
    PbServiceTestCase,
    TidyTCPServer,
    TCPServerTestCase,
)
 
# no init
# pylint: disable=W0232
 
 
class Adder(pb.Referenceable):
    """A remote adder."""
 
    def remote_add(self, first, second):
        """Remote adding numbers."""
        return first + second
 
 
class Calculator(pb.Root):
    """A calculator ran somewhere on the net."""
 
    def __init__(self, adder):
        """Create a new instance."""
         # pb.Root has no __init__
        self.adder = adder
 
    def remote_get_adder(self):
        """Get the remote added."""
        return self.adder
 
    def remote_check_adder(self, other_adder):
        """Check if the are the same."""
        return self.adder == other_adder
 
 
class Echoer(pb.Root):
    """An echoer that repeats what we say."""
 
    def remote_say(self, sentence):
        """Echo what we want to say."""
        return 'Echoer: %s' % sentence
# pylint: enable=W0232
 
class MultipleSercicesTestCase(TestCase):
    """Ensure that several services can be ran."""
 
    timeout = 2
 
    @defer.inlineCallbacks
    def setUp(self):
        """Set the diff tests."""
        yield super(MultipleSercicesTestCase, self).setUp()
        self.first_tcp_server = TidyTCPServer()
        self.second_tcp_server = TidyTCPServer()
        self.adder = Adder()
        self.calculator = Calculator(self.adder)
        self.echoer = Echoer()
 
    @defer.inlineCallbacks
    def test_single_service(self):
        """Test setting a single service."""
        first_number = 1
        second_number = 2
        self.first_tcp_server.listen_server(pb.PBServerFactory,
                                                              self.calculator)
        self.addCleanup(self.first_tcp_server.clean_up)
        calculator_c = self.first_tcp_server.connect_client(pb.PBClientFactory)
        # ensure we do have connected
        yield calculator_c.testserver_on_connection_made
        calculator = yield calculator_c.getRootObject()
        adder = yield calculator.callRemote('get_adder')
        result = yield adder.callRemote('add', first_number, second_number)
        self.assertEqual(first_number + second_number, result)
 
    @defer.inlineCallbacks
    def test_multiple_services(self):
        """Test setting multiple services."""
        first_number = 1
        second_number = 2
        # first service
        self.first_tcp_server.listen_server(pb.PBServerFactory,
                                                              self.calculator)
        self.addCleanup(self.first_tcp_server.clean_up)
 
        # second service
        self.second_tcp_server.listen_server(pb.PBServerFactory, self.echoer)
        self.addCleanup(self.second_tcp_server.clean_up)
 
        # connect the diff clients
        calculator_c = self.first_tcp_server.connect_client(pb.PBClientFactory)
        echoer_c = self.second_tcp_server.connect_client(pb.PBClientFactory)
        # ensure we do have connected
        yield calculator_c.testserver_on_connection_made
        yield echoer_c.testserver_on_connection_made
 
        calculator = yield calculator_c.getRootObject()
        adder = yield calculator.callRemote('get_adder')
        result = yield adder.callRemote('add', first_number, second_number)
        self.assertEqual(first_number + second_number, result)
        echoer = yield echoer_c.getRootObject()
        echo = yield echoer.callRemote('say', 'hello')
        self.assertEqual(self.echoer.remote_say('hello'), echo)

On top of this helper classes I added a twisted site that would follow a similar pattern:

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
"""A tx based web server."""
 
from twisted.internet import defer, reactor, ssl
from twisted.protocols.policies import WrappingFactory
from twisted.web import server
 
from ubuntuone.devtools.testcases.txtcpserver import server_protocol_factory
 
# no init method +  twisted common warnings
# pylint: disable=W0232, C0103, E1101
 
 
class WebServer(object):
    """Webserver used to perform requests in tests."""
 
    def __init__(self, root_resource, ssl_settings=None):
        """Create and start the instance.
 
        The ssl_settings parameter contains a dictionary with the key and cert
        that will be used to perform ssl connections. The root_resource
        contains the resource with all its childre.
        """
        self.root = root_resource
        self.ssl_settings = ssl_settings
        self.port = None
        # use an http.HTTPFactory that was modified to ensure that we have
        # clean close connections
        self.site = server.Site(self.root, timeout=None)
        self.wrapper = WrappingFactory(self.site)
        self.wrapper.testserver_on_connection_lost = defer.Deferred()
        self.wrapper.protocol = server_protocol_factory(self.wrapper.protocol)
        self.wrapper._disconnecting = False
 
    def _listen(self, site, ssl_settings=None):
        """Listen a port to allow the tests."""
        if ssl_settings is None:
            return reactor.listenTCP(0, site)
        else:
            ssl_context = ssl.DefaultOpenSSLContextFactory(
                                    ssl_settings['key'], ssl_settings['cert'])
            return reactor.listenSSL(0, site, ssl_context)
 
    def get_iri(self):
        """Build the iri for this mock server."""
        url = u"http://127.0.0.1:%d/"
        return url % self.get_port()
 
    def get_uri(self):
        """Build the uri for this mock server."""
        url = "http://127.0.0.1:%d/"
        return url % self.get_port()
 
    def get_ssl_iri(self):
        """Build the ssl iri for this mock server."""
        if self.ssl_settings:
            url = u"https://127.0.0.1:%d/"
            return url % self.get_ssl_port()
 
    def get_ssl_uri(self):
        """Build the ssl iri for this mock server."""
        if self.ssl_settings:
            url = "https://127.0.0.1:%d/"
            return url % self.get_ssl_port()
 
    def get_port(self):
        """Return the port where we are listening."""
        return self.port.getHost().port
 
    def get_ssl_port(self):
        """Return the ssl port where we are listening."""
        # pylint: disable=W0212
        if self.ssl_settings:
            return self.port.getHost().port
        # pylint: enable=W0212
 
    def start(self):
        """Start the service."""
        self.port = self._listen(self.wrapper, self.ssl_settings)
 
    def stop(self):
        """Shut it down."""
        if self.port:
            self.wrapper._disconnecting = True
            connected = self.wrapper.protocols.keys()
            if connected:
                for con in connected:
                    con.transport.loseConnection()
            else:
                self.wrapper.testserver_on_connection_lost = 
                                                            defer.succeed(None)
            d = defer.maybeDeferred(self.port.stopListening)
            return defer.gatherResults([d,
                                  self.wrapper.testserver_on_connection_lost])
        return defer.succeed(None)

When using this webserver you have to be careful because we do not longer pay attention to the client protocols, if you do not use twisted to access it you have no problems (libsoup, qtnetwork etc..) but if, for example, you use the HTTPClientFactory you have to do something similar to this in your TestCase setUp:

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
class TxWebClientTestCase(WebClientTestCase):
    """Test case for txweb."""
 
    webclient_factory = txweb.WebClient
 
    @defer.inlineCallbacks
    def setUp(self):
        """Set the diff tests."""
        # delay import, otherwise a default reactor gets installed
        from twisted.web import client
        self.factory = client.HTTPClientFactory
        # set the factory to be used
        # Hook the server's buildProtocol to make the protocol instance
        # accessible to tests and ensure that the reactor is clean!
        build_protocol = self.factory.buildProtocol
        self.serverProtocol = None
 
        def remember_protocol_instance(my_self, addr):
            """Remember the protocol used in the test."""
            protocol = build_protocol(my_self, addr)
            self.serverProtocol = protocol
            on_connection_lost = defer.Deferred()
            connection_lost = protocol.connectionLost
 
            def defer_connection_lost(protocol, *a):
                """Lost connection."""
                if not on_connection_lost.called:
                    on_connection_lost.callback(None)
                connection_lost(protocol, *a)
 
            self.patch(protocol, 'connectionLost', defer_connection_lost)
 
            def cleanup():
                """Clean the connection."""
                if self.serverProtocol.transport is not None:
                    self.serverProtocol.transport.loseConnection()
                return on_connection_lost
 
            self.addCleanup(cleanup)
            return protocol
 
        self.factory.buildProtocol = remember_protocol_instance
        self.addCleanup(self.set_build_protocol, build_protocol)
        txweb.WebClient.client_factory = self.factory
 
        yield super(TxWebClientTestCase, self).setUp()
 
    def set_build_protocol(self, method):
        """Set the method back."""
        self.factory.buildProtocol = method

I have spent some time on this so let me know if you know about any improvements that I can add my brain does not longer want to know about DirtyReactors…

Read more
mandel

Due to a small project I’m developing at the moment I had to access the Mac OS X keyring over python. There is some code of seen around but was not very ‘clean’. The following is my attempt to access the Mac OS X Keyring via python using ctypes, I hope it helps someone out there:

from ctypes import (
    byref,
    c_int32,
    c_uint32,
    c_void_p,
    create_string_buffer,
    memmove,
    cdll,
)
from ctypes.util import find_library
 
from secrets.utils import are_not_none, valid_args
 
 
# Types
 
OSStatus = c_int32
SecKeychainItemRef = c_void_p
SecKeychainRef = c_void_p
 
# Constants
 
errSecSuccess = 0
errSecItemNotFound = -25300
 
 
# Native libraries
 
_security = cdll.LoadLibrary(find_library('Security'))
_core = cdll.LoadLibrary(find_library('CoreServices'))
 
 
def keyring_is_open():
    """Assert that the keyring is not None."""
 
    def keyring_not_none(name, value, method_name='Method'):
        """Ensure that the keyring is open."""
        if not getattr(value, '', None):
            raise OSError('Keyring most be open.')
 
    return valid_args(keyring_not_none, (('Keyring', 0)))
 
 
class Backend(object):
    """Keyring backend for Mac Os X."""
 
    def __init__(self):
        """Create a new Mac OS X backend."""
        super(Backend, self).__init__()
        self.keychain = None
 
    def _get_item(self, realm, username):
        """Return the item that matched the given info."""
        item = SecKeychainItemRef()
        status = _security.SecKeychainFindGenericPassword(self.keychain,
                                         len(realm), realm, len(username),
                                         username, None, None, byref(item))
        return item, status
 
    def open(self):
        """Open the keyring."""
        # we should not be open
        if self.keychain is not None:
            raise OSError()
 
        self.keychain = SecKeychainRef()
        # try to open the keyring
        if _security.SecKeychainOpen('login.keychain', byref(self.keychain)):
            raise OSError("Can't access the keychain")
 
    @keyring_is_open
    def close(self):
        """Close the keyring."""
        _core.CFRelease(self.keychain)
        self.keychain = None
 
    @keyring_is_open
    @are_not_none((
        ('realm', 1),
        ('username', 2),
        ('password', 3)))
    def set_password(self, realm, username, password):
        """Set the password for the given real and username."""
        item, status = self._get_item(realm, username)
 
        if status == errSecItemNotFound:
            status = _security.SecKeychainAddGenericPassword(self.keychain,
                                                len(realm), realm,
                                                len(username), username,
                                                len(password), password, None)
            if status:
                raise OSError()
        else:
            raise OSError()
 
    @keyring_is_open
    @are_not_none((
        ('realm', 1),
        ('username', 2),
        ('password', 3)))
    def update_password(self, realm, username, password):
        """Update the password for the given realm."""
        item, status = self._get_item(realm, username)
 
        if status and status == errSecItemNotFound:
            raise OSError("Can't store password in keychain")
        else:
            status = _security.SecKeychainItemModifyAttributesAndData(item, None,
                                                len(password), password)
            _core.CFRelease(item)
            if status:
                raise OSError()
        if status:
            raise OSError("Can't store password in keychain")
 
    @keyring_is_open
    @are_not_none((
        ('realm', 1),
        ('username', 2)))
    def get_password(self, realm, username):
        """Get the password for the given real and username."""
        length = c_uint32()
        data = c_void_p()
        status = _security.SecKeychainFindGenericPassword(self.keychain,
                                          len(realm), realm,
                                          len(username), username,
                                          byref(length), byref(data), None)
        if status == errSecSuccess:
            password = create_string_buffer(length.value)
            memmove(password, data.value, length.value)
            password = password.raw
            _security.SecKeychainItemFreeContent(None, data)
        elif status == errSecItemNotFound:
            password = None
        else:
            raise OSError("Can't fetch password from system")
        return password
 
    @keyring_is_open
    @are_not_none((
        ('realm', 1),
        ('username', 2)))
    def delete_password(self, realm, username):
        """Delete the password of the given real and username."""
        item, status = self._get_item(realm, username)
        if status == errSecSuccess:
            _security.SecKeychainItemDelete(item)
            _core.CFRelease(item)
        else:
            raise OSError()

I have not added the code of the decorator because they are just noise, the only thing they do is to check that the keyring was indeed opened (self.keyring != None) and that the parameters with the given index are not None (I’m lazy and I prefer to use decorators for this mundane tasks that are done everywhere.

Read more
mandel

On Windows Ubuntu One uses the twisted reactor to run the Qt UI. The main reason for this is that the IPC protocol that is used was written in twisted. This has been giving us a number of head aches like the one we experienced today.

The following code simply shows a dialog that will as the user for his proxy credentials and will store them in the key-ring of which ever platform is being used:

def main():
    """Main method used to show the creds dialog."""
 
    if sys.platform == 'win32':
        import qt4reactor
        qt4reactor.install()
        logger.debug('Qt reactor installed.')
 
    app = QApplication(sys.argv)
    args = parse_args()
    win = ProxyCredsDialog(domain=args.domain,
                           retry=args.retry)
    return_code = win.exec_()
    if sys.platform == 'win32':
        from twisted.internet import reactor
        reactor.run()
    sys.exit(return_code)

From the dialog the most interesting part is the one in which the credentials are stored:

@defer.inlineCallbacks
def _on_save_clicked(self, *args):
    """Save the new credentials."""
    username = unicode(self.ui.username_entry.text()).encode('utf8')
    password = unicode(self.ui.password_entry.text()).encode('utf8')
    creds = dict(username=username, password=password)
    try:
        logger.debug('Save credentials as for domain %s.', self.domain)
        yield self.keyring.set_credentials(self.domain, creds)
    except Exception, e:
        logger.exception('Could not set credentials:')
        self.done(EXCEPTION_RAISED)
    logger.debug('Stored creds')
    self.done(USER_SUCCESS)

And to give even more details, the following is what is used to spawn a thread to store the credentials on windows:

def set_credentials(self, app_name, cred):
    """Set the credentials of the Ubuntu SSO item."""
    # the windows keyring can only store a pair username-password
    # so we store the data using ubuntu_sso as the user name. Then
    # the cred will be stored as the string representation of the dict.
    return deferToThread(self.keyring.set_password, app_name, USERNAME,
                             dumps(cred))

A priori there is nothing wrong with the code, or is it? Doing an IRL test you will see that the credentials are never stored, what’s even more none of the deferreds are called. But why? In theory the qt reactor should be taking care of everything which includes the deferreds, the deferToThread and the execution of the ui.. well, it is not. When we look a little closer we can see that we use the exec_ method from the QDialog and this is the root of the bug. Lets put an example, the following is possible in Qt:

import sys
 
from PyQt4.QtGui import QApplication, QDialog
 
app = QApplication(sys.argv)
dialog = QDialog()
dialog.exec_()

As you can see we are launching the dialog but we did not execute the application, but why is that? The main reason is found in the implementation of exec in the QDialog class (this time in C++, ouch!):

int QDialog::exec()
{
     Q_D(QDialog);
     if (d->eventLoop) {
         qWarning("QDialog::exec: Recursive call detected");
         return -1;
     }
 
     bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
     setAttribute(Qt::WA_DeleteOnClose, false);
 
     bool wasShowModal = testAttribute(Qt::WA_ShowModal);
     setAttribute(Qt::WA_ShowModal, true);
     setResult(0);
 
     show();
 
     QEventLoop eventLoop;
     d->eventLoop = &eventLoop;
     (void) eventLoop.exec();
     d->eventLoop = 0;
 
     setAttribute(Qt::WA_ShowModal, wasShowModal);
 
     int res = result();
     if (deleteOnClose)
         delete this;
     return res;
 }

As you can see the implementation uses a QEventLoop, if you read the documentation you will noticed that using exec of the event loops and the default flag all the events will me processed by this event loop, which is not the main event loop, but a child one (small brain fuck here). This means that, due to the implementation of the qtreactor, when using a dialog exec_ (or better say a QEventLoop.exec method) the reactor main loop is not processing the events which means that all your deferreds, deferToThread etc.. will not be processed :(

Nevertheless there is a way to work around this bug in the qtreactor implementation (because is not the qt fault and it is well documented) which is doing the following workaround:

win = ProxyCredsDialog(domain=args.domain, retry=args.retry)
win.show()
win.finished.connect(exit_app)

The above code ensures that the events will be handeled by the reactor main loop and not by a child QEventLoop.

In summary, qtreactor is buggy and will give you problems.. but if you really have to use it (like we do) do remember this detail, DO NOT use exec.

Read more
mandel

I have been writing some integration tests lately between Ubuntu One and proxies which use SSL certificates. The idea behind this tests was to be able to test that we deal correctly with those certificates that are not correct (notify the user, remember exceptions, etc..) For that I wrote this small function that I used to generate the certificates.

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
import os
 
from socket import gethostname
 
from OpenSSL import crypto
 
 
def generate_self_signed_cert(cert_dir, is_valid=True):
    """Generate a SSL certificate.
 
    If the cert_path and the key_path are present they will be overwritten.
    """
    if not os.path.exists(cert_dir):
        os.makedirs(cert_dir)
    cert_path = os.path.join(cert_dir, 'squid.crt')
    key_path = os.path.join(cert_dir, 'squid.key')
 
    if os.path.exists(cert_path):
        os.unlink(cert_path)
    if os.path.exists(key_path):
        os.unlink(key_path)
 
    # create a key pair
    key = crypto.PKey()
    key.generate_key(crypto.TYPE_RSA, 1024)
 
    # create a self-signed cert
    cert = crypto.X509()
    cert.get_subject().C = 'UK'
    cert.get_subject().ST = 'London'
    cert.get_subject().L = 'London'
    cert.get_subject().O = 'Canonical'
    cert.get_subject().OU = 'Ubuntu One'
    cert.get_subject().CN = gethostname() if is_valid else gethostname()[::-1]
    cert.set_serial_number(1000)
    cert.gmtime_adj_notBefore(0)
    cert.gmtime_adj_notAfter(10 * 365 * 24 * 60 * 60) 
    cert.set_issuer(cert.get_subject())
    cert.set_pubkey(key)
    cert.sign(key, 'sha1')
 
    with open(cert_path, 'wt') as fd: 
        fd.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
 
    with open(key_path, 'wt') as fd: 
        fd.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
 
    return cert_path, key_path

I leave to the reader to modify the function to match their needs.

Read more
mandel

Recently in Ubuntu One we have been working or using PyQt for the UI of our application so that we could keep a smaller code base. While doing the move I noticed that we needed to have a widget similar to GtkArrow and to my surprise there is not one.

The following is a small implementation fo such a widget that might help other people that are in need of it. Even maybe someone that cares enough will write it in C++ and will propose it to Qt, sorry but I don’t have the time.

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
 
from PyQt4.QtGui import QPainter, QStyle, QStyleOption, QWidget
 
 
class QArrow(QWidget):
    """Qt implementation similar to GtkArrow."""
 
   UP = 0
   DOWN = 1
   LEFT = 2
   RIGHT = 3
 
   def __init__(self, direction, parent=None):
       """Create a new instance."""
       QWidget.__init__(self, parent)
       if not direction in (QArrow.UP, QArrow.DOWN,
                 QArrow.LEFT, QArrow.RIGHT):
           raise ValueError('Wrong arrow direction.')
       self._direction = direction
 
    def paintEvent(self, event):
        """Paint a nice primitive arrow."""
        opt = QStyleOption()
        opt.initFrom(self)
        p = QPainter(self)
        if self._direction == QArrow.UP:
            primitive = QStyle.PE_IndicatorArrowUp
        elif self._direction == QArrow.DOWN:
            primitive = QStyle.PE_IndicatorArrowDown
        elif self._direction == QArrow.LEFT:
            primitive = QStyle.PE_IndicatorArrowLeft
        else:
            primitive = QStyle.PE_IndicatorArrowRight
        self.style().drawPrimitive(primitive, opt, p, self)

Took longer to think the correct way to do it than the actual coding, at the end is very simple.

Read more
mandel

I have been playing around with jquery a little lately and I found that it is not very well documented how to add an input field like a checkbox inside the header of an accordion so that you can click on the header and the accordeon works as expected and the checkbox can be clicked. I’m not an expert in this things but I found the way to work around the issue:

The HTML:

1
2
3
4
5
6
7
8
9
10
11
12
<div id="accordion">
    <div>
        <h3 id='example'>
            <a href='#'>
                <label>Title<input class='header-checkbox' type='checkbox' /></label>
            </a>
        </h3>
        <div>
            <p></p>
        </div>
    </div>
</div>

The Javascript:

$('#example').find('input').click(
    function(e){
        e.stopPropagation();
    }
);

I’m sure this is very easy for any advance js guy, but since I googled and did not find it I though I would be a nice guy and post it here for the next guy.

Read more
mandel

Yesterday I was working on a small UI that uses a WebKit.WebView to show certain information to the user. My idea yesterday was to try and show a native context menu on the view that does not have the default menu items that the WebView has, the only way I’ve found to this is a as follows:

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
from gi.repository import GObject, Gdk, Gtk, WebKit
from utils import get_data_file
 
 
class CustomView(Gtk.EventBox):
    """Main window used to show the ui and generate signals."""
 
    def __init__(self):
        """Create a new instance."""
        super(CustomView, self).__init__()
        self.set_events(Gdk.EventMask.BUTTON_PRESS_MASK)
        self._create_context_menu()
        self.scroll = Gtk.ScrolledWindow()
        self.scroll.show()
        self.view = WebKit.WebView()
        self.view.show()
        self.scroll.add(self.view)
        # load the html used for the main window, from that point
        # on we will let the html and js do the work and raise
        # signals via the title
        self.view.open(get_data_file(MAIN_WINDOW_HMTL))
        # lets no use the webkit context menu
        settings = self.view.get_settings()
        settings.set_property('enable-default-context-menu',
                              False)
        self.view.set_settings(settings)
        self.add(self.scroll)
        # connect the on button pressed event of the event box
        # so that we can add a context menu
        self.connect('button_press_event', self._on_button_press_event)
 
    def _create_context_menu(self):
        """Create the context menu."""
        self.menu = Gtk.Menu()
        delete_menu = Gtk.MenuItem("Delete Task")
        self.menu.append(delete_menu)
 
    def _on_button_press_event(self, widget, event):
        """Deal with the button press event."""
        if event.button == 3:
            self.menu.popup(None, None, None, None, event.button, event.time)
            self.menu.show_all()

Does any one out there know a better way to do this. I know that there is a “populate-popup” signal I can listen to but I cannot get my head around on how to do exactly what I want, which is remove all defaul menu items and add my own. And, does it make sense to have to do that for every popup signal?

Read more
mandel

With GObject introspection is very simple to set the settings of your system trough python. Fist, lets use the command line to find out our current settings:

gsettings list-recursively org.gnome.system.proxy

The following script allows you to retrieve the http proxy settings that you are currently using:

from gi.repository import Gio
 
def get_settings():
    """Get proxy settings."""
    http_settings = Gio.Settings.new('org.gnome.system.proxy.http')
    host = http_settings.get_string('host')
    port = http_settings.get_int('port')
    if http_settings.get_boolean('use-authentication'):
        username = http_settings.get_string('authentication_user')
        password = http_settings.get_string('authentication_password')
    else:
        username = password = None
    return host, port, username, password

Setting them is as easy as getting them:

from gi.repository import Gio
 
def set_settings(host, port, username=None, password=None):
     """Set proxy settings."""
     http_settings = Gio.Settings.new('org.gnome.system.proxy.http')
     http_settings.set_string('host', host)
     http_settings.set_int('port', port)
     if username is not None:
         http_settings.set_boolean('use-authentication', True)
         http_settings.set_string('authentication_user', username)
         http_settings.set_string('authentication_password', password)

This is not utterly complicated but I’m notice that there are not many examples out there, so there you go. There is no code there that can be considered hard but I’d like to point out that if you use the get_value method from the Settings object you will have to call the appropriate get_* method from the returned GVariant, that is:

host = http_settings.get_string('host')

is equal to the following:

host = http_settings.get_value('host').get_string()

Read more