#
# This file is part of Gruvi. Gruvi is free software available under the
# terms of the MIT license. See the file "LICENSE" that was provided
# together with this source file for the licensing terms.
#
# Copyright (c) 2012-2014 the Gruvi authors. See the file "AUTHORS" for a
# complete list.

from __future__ import absolute_import, print_function, division

import random
import unittest

import gruvi
from gruvi import Fiber, Event, Process, Timeout
from gruvi.futures import FiberPool, ThreadPool, Future
from support import UnitTest


class TestFuture(UnitTest):

    def test_result(self):
        fut = Future()
        fut.set_result(10)
        self.assertEqual(fut.result(), 10)
        self.assertIsNone(fut.exception())
        self.assertEqual(fut.result(), 10)

    def test_exception(self):
        fut = Future()
        fut.set_exception(ValueError())
        self.assertIsInstance(fut.exception(), ValueError)
        self.assertRaises(ValueError, fut.result)

    def test_wait_result(self):
        fut = Future()
        def set_result():
            gruvi.sleep(0.1)
            fut.set_result('foo')