# Buggy code
def checkpoint(self):
    """
    Checkpoint this download. Returns a deferred that fires when the checkpointing is completed.
    """
    if self._checkpoint_disabled:
        self._logger.warning(
            "Ignoring checkpoint() call as checkpointing is disabled for this download"
        )
        return succeed(None)

    if not self.handle or not self.handle.is_valid():
        resume_data = {
            "file-format": "libtorrent resume file",
            "file-version": 1,
            "info-hash": self.tdef.get_infohash(),
        }
        alert = type("anonymous_alert", (object,), dict(resume_data=resume_data))
        self.on_save_resume_data_alert(alert)
        return succeed(None)

    return self.save_resume_data()


# Traceback (AssertionError)
Tuples differ: (2, ('127.0.0.1', [5, 4, 3, 2,... != (2, ('127.0.0.1', [5, 4, 3, 2,...

First differing element 2:
u'None'
''

- (2, ('127.0.0.1', [5, 4, 3, 2, 1]), u'None')
?                                     - ----

+ (2, ('127.0.0.1', [5, 4, 3, 2, 1]), '')
Stacktrace

Traceback (most recent call last):
File "c:\python27\lib\unittest\case.py", line 329, in run
testMethod()
File "C:\workspace\jenkins\workspace\GH_Tribler_PR_tests_win64\tribler\Tribler\Test\test_as_server.py", line 58, in check
result = fun(*argv, **kwargs)
File "C:\workspace\jenkins\workspace\GH_Tribler_PR_tests_win64\tribler\Tribler\Test\Core\Upgrade\test_config_upgrade_70_71.py", line 33, in test_read_test_libtribler_conf
self.assertEqual(result_config.get_anon_proxy_settings(), (2, ("127.0.0.1", [5, 4, 3, 2, 1]), ''))
File "c:\python27\lib\unittest\case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "c:\python27\lib\unittest\case.py", line 753, in assertTupleEqual
self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)
File "c:\python27\lib\unittest\case.py", line 724, in assertSequenceEqual
self.fail(msg)
File "c:\python27\lib\unittest\case.py", line 410, in fail
raise self.failureException(msg)
AssertionError: Tuples differ: (2, ('127.0.0.1', [5, 4, 3, 2,... != (2, ('127.0.0.1', [5, 4, 3, 2,...

First differing element 2:
u'None'
''

- (2, ('127.0.0.1', [5, 4, 3, 2, 1]), u'None')
?                                     - ----

+ (2, ('127.0.0.1', [5, 4, 3, 2, 1]), '')

# Fixed code
def checkpoint(self):
    """
    Checkpoint this download. Returns a deferred that fires when the checkpointing is completed.
    """
    if self._checkpoint_disabled:
        self._logger.warning(
            "Ignoring checkpoint() call as checkpointing is disabled for this download"
        )
        return succeed(None)

    if not self.handle or not self.handle.is_valid():
        # Libtorrent hasn't received or initialized this download yet
        # 1. Check if we have data for this infohash already (don't overwrite it if we do!)
        basename = hexlify(self.tdef.get_infohash()) + ".state"
        filename = os.path.join(self.session.get_downloads_pstate_dir(), basename)
        if not os.path.isfile(filename):
            # 2. If there is no saved data for this infohash, checkpoint it without data so we do not
            #    lose it when we crash or restart before the download becomes known.
            resume_data = {
                "file-format": "libtorrent resume file",
                "file-version": 1,
                "info-hash": self.tdef.get_infohash(),
            }
            alert = type("anonymous_alert", (object,), dict(resume_data=resume_data))
            self.on_save_resume_data_alert(alert)
        return succeed(None)

    return self.save_resume_data()